【文件属性】:
文件名称:ios 录音文件caf转mp3
文件大小:639KB
文件格式:ZIP
更新时间:2018-06-01 05:12:55
caf MP3 录音 ios
ios录音的caf文件转MP3文件,以兼容android
注意音频参数的设置,如果声音异常,请调整参数。
code:
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *sessionError;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
_sampleRate = 11025;//8000;//44100;
_quality = AVAudioQualityLow;
if(session == nil)
NSLog(@"Error creating session: %@", [sessionError description]);
else
[session setActive:YES error:nil];
NSString *cafFilePath = strCaf;
// NSString *mp3FileName = @"Mp3File";
// mp3FileName = [mp3FileName stringByAppendingString:@".mp3"];
NSString *mp3FilePath = strMP3Path;//[[NSHomeDirectory() stringByAppendingFormat:@"/Documents/"] stringByAppendingPathComponent:mp3FileName];
@try {
int read, write;
FILE *pcm = fopen([cafFilePath cStringUsingEncoding:1], "rb"); //source
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb"); //output
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, _sampleRate);
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
{
}
【文件预览】:
libmp3lame.a
lame.h
网友评论
- 就是lame库
- 好用不错。。
- 之前不会,看完就会了,真的学习了