试了很多次,腾讯云录音时分辨率需要设置22050.0
@try {
FILE*fwav =fopen([wavPathc StringUsingEncoding:NSASCIIStringEncoding],"rb");
fseek(fwav, 1024*4, SEEK_CUR); //跳过源文件的信息头,不然在开头会有爆破音
FILE*fmp3 =fopen([mp3Path cStringUsingEncoding:NSASCIIStringEncoding],"wb");
lame=lame_init();
lame_set_in_samplerate(lame, 22050.0); //设置wav的采样率
// lame_set_num_channels(lame, 2); //声道,不设置默认为双声道
lame_set_brate(lame, 16);//比特率
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);
// const int PCM_SIZE = 640 * 2; //双声道*2 单声道640即可
// const int MP3_SIZE = 8800; //计算公式wav_buffer.length * 1.25 + 7200
const intPCM_SIZE =8192;//双声道*2 单声道640即可
const int MP3_SIZE = 8192; //计算公式wav_buffer.length * 1.25 + 7200
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];
int read, write;
do{
read =fread(pcm_buffer,sizeof(shortint), PCM_SIZE, fwav);
if(read ==0) {
write =lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
}else{
write =lame_encode_buffer_interleaved(lame, pcm_buffer, read/2, mp3_buffer, MP3_SIZE);
// write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
}
fwrite(mp3_buffer, write,1, fmp3);
}while(read !=0);
lame_close(lame);
fclose(fmp3);
fclose(fwav);
}@catch(NSException *exception) {
NSLog(@"catch exception");
}@finally{
block();
}