由于采用手機(jī)端系統(tǒng)的錄音功能响疚,為了讓iOS和Android的錄音在其他平臺(tái)上能夠正常播放,這是就需要對(duì)錄音的文件進(jìn)行轉(zhuǎn)碼瞪醋,一般轉(zhuǎn)成MP3格式忿晕,這里轉(zhuǎn)碼用的三方庫(kù)是lame
ios端對(duì)于錄音轉(zhuǎn)碼的源碼如下:
//音頻轉(zhuǎn)碼
- (void)audio_mp3
{
NSString *cafFilePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];
cafFilePath = [cafFilePath stringByAppendingPathComponent:kRecordAudioFile];
NSString *mp3FilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];
mp3FilePath = [mp3FilePath stringByAppendingPathComponent:@"myRecord1.mp3"];
@try{
intread, write;
FILE*pcm =fopen([cafFilePath cStringUsingEncoding:NSASCIIStringEncoding],"rb");
fseek(pcm,4*1024,SEEK_CUR);
FILE*mp3 =fopen([mp3FilePath cStringUsingEncoding:NSASCIIStringEncoding],"wb");
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 = (int)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{
NSLog(@"MP3生成成功: %@",mp3FilePath);
}
}
音頻轉(zhuǎn)碼的代碼就這些,希望能對(duì)大家有一點(diǎn)的幫助银受。