iOS 录音转码MP3及转码BASE64上传
一,开始录音
NSLog(@"开始录音");
[self startRecord];
1 - (void)startRecord 2 { 3 //删除上次生成的文件,保留最新文件 4 NSFileManager *fileManager = [NSFileManager defaultManager]; 5 if ([NSTemporaryDirectory() stringByAppendingString:@"myselfRecord.mp3"]) { 6 [fileManager removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:@"myselfRecord.mp3"] error:nil]; 7 } 8 if ([NSTemporaryDirectory() stringByAppendingString:@"selfRecord.wav"]) { 9 [fileManager removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:@"selfRecord.wav"] error:nil]; 10 } 11 12 //开始录音 13 //录音设置 14 NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init]; 15 //设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM 16 [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; 17 //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量), 采样率必须要设为11025才能使转化成mp3格式后不会失真 18 [recordSetting setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey]; 19 //录音通道数 1 或 2 ,要转换成mp3格式必须为双通道 20 [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey]; 21 //线性采样位数 8、16、24、32 22 [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; 23 //录音的质量 24 [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey]; 25 26 //存储录音文件 27 recordUrl = [NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingString:@"selfRecord.wav"]]; 28 29 //初始化 30 audioRecorder = [[AVAudioRecorder alloc] initWithURL:recordUrl settings:recordSetting error:nil]; 31 //开启音量检测 32 audioRecorder.meteringEnabled = YES; 33 audioSession = [AVAudioSession sharedInstance];//得到AVAudioSession单例对象 34 35 if (![audioRecorder isRecording]) { 36 [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];//设置类别,表示该应用同时支持播放和录音 37 [audioSession setActive:YES error:nil];//启动音频会话管理,此时会阻断后台音乐的播放. 38 39 [audioRecorder prepareToRecord]; 40 [audioRecorder peakPowerForChannel:0.0]; 41 [audioRecorder record]; 42 } 43 }
二,停止录音
[self endRecord];
1 - (void)endRecord 2 { 3 [audioRecorder stop]; //录音停止 4 [audioSession setActive:NO error:nil]; //一定要在录音停止以后再关闭音频会话管理(否则会报错),此时会延续后台音乐播放 5 }
三,转码成MP3
1 - (void)transformCAFToMP3 { 2 mp3FilePath = [NSURL URLWithString:[NSTemporaryDirectory() stringByAppendingString:@"myselfRecord.mp3"]]; 3 4 @try { 5 int read, write; 6 7 FILE *pcm = fopen([[recordUrl absoluteString] cStringUsingEncoding:1], "rb"); //source 被转换的音频文件位置 8 fseek(pcm, 4*1024, SEEK_CUR); //skip file header 9 FILE *mp3 = fopen([[mp3FilePath absoluteString] cStringUsingEncoding:1], "wb"); //output 输出生成的Mp3文件位置 10 11 const int PCM_SIZE = 8192; 12 const int MP3_SIZE = 8192; 13 short int pcm_buffer[PCM_SIZE*2]; 14 unsigned char mp3_buffer[MP3_SIZE]; 15 16 lame_t lame = lame_init(); 17 lame_set_in_samplerate(lame, 11025.0); 18 lame_set_VBR(lame, vbr_default); 19 lame_init_params(lame); 20 21 do { 22 read = (int)fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm); 23 if (read == 0) 24 write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE); 25 else 26 write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE); 27 28 fwrite(mp3_buffer, write, 1, mp3); 29 30 } while (read != 0); 31 32 lame_close(lame); 33 fclose(mp3); 34 fclose(pcm); 35 } 36 @catch (NSException *exception) { 37 NSLog(@"%@",[exception description]); 38 } 39 @finally { 40 NSLog(@"MP3生成成功"); 41 base64Str = [self mp3ToBASE64]; 42 } 43 }
四,上传需要转码BASE64
1 - (NSString *)mp3ToBASE64{ 2 NSData *mp3Data = [NSData dataWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingString:@"myselfRecord.mp3"]]; 3 NSString *_encodedImageStr = [mp3Data base64Encoding]; 4 NSLog(@"===Encoded image:\n%@", _encodedImageStr); 5 return _encodedImageStr; 6 }
备注:其中可以直接生成的.caf .wav 有压缩的MP3需要转格式,不能直接录音生成