ios用lame把wav转换成MP3格式获取时间不对解决方案


最近在项目中用到lame转码将wav转换成MP3格式,其中遇到了不少的坑,最大的一个就是转码完成后, 用audioPlayer.duration获取的时间不准,而且获取的时间一直在变,最后在一位大神的帮助下解决了这个问题,下面就分享下解决过程(把整个转码过程都说一遍)!

我从录制开始讲:

首先在录音时要设置录音参数    记住了,通道数目必须设置为2,否则转码后声音不对

//录音设置  
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];  
//录音格式 无法使用  
[recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];  
//采样率  
[recordSettings setValue :[NSNumber numberWithFloat:11025.0] forKey: AVSampleRateKey];//44100.0  
//通道数  
[recordSettings setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];  
//线性采样位数  
//[recordSettings setValue :[NSNumber numberWithInt:16] forKey: AVLinearPCMBitDepthKey];  
//音频质量,采样质量  
[recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];  

接着是转码了  
注意:注意了:录音的采样率和转码的采样率必须一样   必须一样

- (void)audio_PCMtoMP3  
{  
  
    NSString *mp3FileName = [self.audioFileSavePath lastPathComponent];  
    mp3FileName = [mp3FileName stringByAppendingString:@".mp3"];  
    NSString *mp3FilePath = [self.audioTemporarySavePath stringByAppendingPathComponent:mp3FileName];  
      
    @try {  
        int read, write;  
          
        FILE *pcm = fopen([self.audioFileSavePath cStringUsingEncoding:1], "rb");  //source 被转换的音频文件位置  
        fseek(pcm, 4*1024, SEEK_CUR);                                   //skip file header  
        FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb");  //output 输出生成的Mp3文件位置  
          
        const int PCM_SIZE = 8192;  
        const int MP3_SIZE = 8192;  
        short int pcm_buffer[PCM_SIZE*2];  
        unsigned char mp3_buffer[MP3_SIZE];  
          
        lame_t lame = lame_init();  
        lame_set_in_samplerate(lame, 11025.0);  
        lame_set_VBR(lame, vbr_default);  
        lame_init_params(lame);  
          
        do {  
            read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);  
            if (read == 0)  
                write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);  
            else  
                write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);  
              
            fwrite(mp3_buffer, write, 1, mp3);  
              
        } while (read != 0);  
          
        lame_close(lame);  
        fclose(mp3);  
        fclose(pcm);  
    }  
    @catch (NSException *exception) {  
        NSLog(@"%@",[exception description]);  
    }  
    @finally {  
        self.audioFileSavePath = mp3FilePath;  
        NSLog(@"MP3生成成功: %@",self.audioFileSavePath);  
    }  
  
} 

(上面的代码都是网上copy的,重点看参数的设置)转码成功后,如果你用 audioPlayer.duration获取播放时间,你会发现时间不准 

看解决方案:

//只有这个方法获取时间是准确的 audioPlayer.duration获得的时间不准
    AVURLAsset* audioAsset =[AVURLAsset URLAssetWithURL:fileUrl options:nil];
    CMTime audioDuration = audioAsset.duration;
    float audioDurationSeconds =CMTimeGetSeconds(audioDuration);

用上面这个方法,你就会发现获取的时间准了,哈哈 解决问题!!!!!

    原文作者:羽辰追梦
    原文地址: https://blog.csdn.net/sg_zxw/article/details/54948592
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞