iOS实现录音转码MP3及转码BASE64上传示例
iOS录音转码MP3及转码BASE64上传
一,开始录音
NSLog(@"开始录音"); [selfstartRecord]; -(void)startRecord { //删除上次生成的文件,保留最新文件 NSFileManager*fileManager=[NSFileManagerdefaultManager]; if([NSTemporaryDirectory()stringByAppendingString:@"myselfRecord.mp3"]){ [fileManagerremoveItemAtPath:[NSTemporaryDirectory()stringByAppendingString:@"myselfRecord.mp3"]error:nil]; } if([NSTemporaryDirectory()stringByAppendingString:@"selfRecord.wav"]){ [fileManagerremoveItemAtPath:[NSTemporaryDirectory()stringByAppendingString:@"selfRecord.wav"]error:nil]; } //开始录音 //录音设置 NSMutableDictionary*recordSetting=[[NSMutableDictionaryalloc]init]; //设置录音格式AVFormatIDKey==kAudioFormatLinearPCM [recordSettingsetValue:[NSNumbernumberWithInt:kAudioFormatLinearPCM]forKey:AVFormatIDKey]; //设置录音采样率(Hz)如:AVSampleRateKey==8000/44100/96000(影响音频的质量),采样率必须要设为11025才能使转化成mp3格式后不会失真 [recordSettingsetValue:[NSNumbernumberWithFloat:11025.0]forKey:AVSampleRateKey]; //录音通道数1或2,要转换成mp3格式必须为双通道 [recordSettingsetValue:[NSNumbernumberWithInt:2]forKey:AVNumberOfChannelsKey]; //线性采样位数8、16、24、32 [recordSettingsetValue:[NSNumbernumberWithInt:16]forKey:AVLinearPCMBitDepthKey]; //录音的质量 [recordSettingsetValue:[NSNumbernumberWithInt:AVAudioQualityHigh]forKey:AVEncoderAudioQualityKey]; //存储录音文件 recordUrl=[NSURLURLWithString:[NSTemporaryDirectory()stringByAppendingString:@"selfRecord.wav"]]; //初始化 audioRecorder=[[AVAudioRecorderalloc]initWithURL:recordUrlsettings:recordSettingerror:nil]; //开启音量检测 audioRecorder.meteringEnabled=YES; audioSession=[AVAudioSessionsharedInstance];//得到AVAudioSession单例对象 if(![audioRecorderisRecording]){ [audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecorderror:nil];//设置类别,表示该应用同时支持播放和录音 [audioSessionsetActive:YESerror:nil];//启动音频会话管理,此时会阻断后台音乐的播放. [audioRecorderprepareToRecord]; [audioRecorderpeakPowerForChannel:0.0]; [audioRecorderrecord]; } }
二,停止录音
[selfendRecord]; -(void)endRecord { [audioRecorderstop];//录音停止 [audioSessionsetActive:NOerror:nil];//一定要在录音停止以后再关闭音频会话管理(否则会报错),此时会延续后台音乐播放 }
三,转码成MP3
-(void)transformCAFToMP3{ mp3FilePath=[NSURLURLWithString:[NSTemporaryDirectory()stringByAppendingString:@"myselfRecord.mp3"]]; @try{ intread,write; FILE*pcm=fopen([[recordUrlabsoluteString]cStringUsingEncoding:1],"rb");//source被转换的音频文件位置 fseek(pcm,4*1024,SEEK_CUR);//skipfileheader FILE*mp3=fopen([[mp3FilePathabsoluteString]cStringUsingEncoding:1],"wb");//output输出生成的Mp3文件位置 constintPCM_SIZE=8192; constintMP3_SIZE=8192; shortintpcm_buffer[PCM_SIZE*2]; unsignedcharmp3_buffer[MP3_SIZE]; lame_tlame=lame_init(); lame_set_in_samplerate(lame,11025.0); lame_set_VBR(lame,vbr_default); lame_init_params(lame); do{ read=(int)fread(pcm_buffer,2*sizeof(shortint),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(@"%@",[exceptiondescription]); } @finally{ NSLog(@"MP3生成成功"); base64Str=[selfmp3ToBASE64]; } }
四,上传需要转码BASE64
-(NSString*)mp3ToBASE64{ NSData*mp3Data=[NSDatadataWithContentsOfFile:[NSTemporaryDirectory()stringByAppendingString:@"myselfRecord.mp3"]]; NSString*_encodedImageStr=[mp3Database64Encoding]; NSLog(@"===Encodedimage:\n%@",_encodedImageStr); return_encodedImageStr; }
备注:其中可以直接生成的.caf .wav 有压缩的MP3需要转格式,不能直接录音生成
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。