Use a third -party lame ,Mp3 Audio encoder.
Before using Avaudiorecorder for audio recording, set the parameters:
NSString *recordtemporarypathstring = [NSString stringwithformat:@ "%@/temporary", Self.audiotemporarysavepath];
LINEARPCM is a lossless encoding format for iOS, but it's bulky
Recording settings
Nsmutabledictionary *recordsettings = [[Nsmutabledictionary alloc] init];
Recording format not available
[Recordsettings setValue: [NSNumber NUMBERWITHINT:KAUDIOFORMATLINEARPCM] forkey:avformatidkey];
Sample Rate
[Recordsettings setValue: [NSNumber numberwithfloat:11025.0] forkey:avsampleratekey];//44100.0
Number of channels
[Recordsettings setValue: [NSNumber numberwithint:2] forkey:avnumberofchannelskey];
Number of linear sample bits
[Recordsettings setValue: [NSNumber numberwithint:16] forkey:avlinearpcmbitdepthkey];
Audio quality, sampling quality
[Recordsettings Setvalue:[nsnumber numberwithint:avaudioqualitymin] forkey:avencoderaudioqualitykey];
#include "lame.h"
-(void) Audio_pcmtomp3
{
NSString *mp3filename = [Self.audiofilesavepath lastpathcomponent];
Mp3filename = [Mp3filename stringbyappendingstring:@ ". mp3"];
NSString *mp3filepath = [Self.audiotemporarysavepath stringbyappendingpathcomponent:mp3filename];
@try {
int read, write;
FILE *PCM = fopen ([Self.audiofilesavepath cstringusingencoding:1], "RB"); Source converted audio File location
Fseek (PCM, 4*1024, seek_cur); Skip File Header
FILE *mp3 = fopen ([Mp3filepath cstringusingencoding:1], "WB"); MP3 file location generated by output outputs
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 = 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 {
Self.audiofilesavepath = Mp3filepath;
NSLog (@ "MP3 generated successfully:%@", Self.audiofilesavepath);
}
}
更多iOS疯狂详解:http://blog.csdn.net/wanglongblog
iOS crazy detailed recording audio converted into MP3