1.使用lame库实现pcm和mp3格式转换,官网http://lame.sourceforge.net/下载lame.h和libmp3lame.a库文件
intread, write;
FILE*pcm =fopen([WAVFilePathcStringUsingEncoding:1],"rb");//source被转换的音频文件位置
fseek(pcm,4*1024,SEEK_CUR);//skip file header
FILE*mp3 =fopen([mp3FilePathcStringUsingEncoding: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,22050.0);//采样率22050.0
lame_set_VBR(lame,vbr_default);
lame_init_params(lame);
do{
read =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);