IOS crazy explanation-converting recording audio into Mp3
Use a third partyLame,Mp3Audio 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 is bulky.
// Recording settings
NSMutableDictionary * recordsettionary = [[NSMutableDictionary alloc] init];
// The recording format cannot be used.
[RecordSettings setValue: [NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
// Sampling Rate
[Recordsetasksetvalue: [NSNumber numberWithFloat: 11025.0] forKey: AVSampleRateKey]; // 44100.0
// Number of channels
[Recordsetasksetvalue: [NSNumber numberWithInt: 2] forKey: AVNumberOfChannelsKey];
// Number of linear sampling digits
// [Recordsetasksetvalue: [NSNumber numberWithInt: 16] forKey: AVLinearPCMBitDepthKey];
// Audio quality and sampling quality
[RecordSettings setValue: [NSNumber numberWithInt: AVAudioQualityMin] forKey: AVEncoderAudioQualityKey];
# Include lame. h
-(Void) audio_PCMtoMP3
{
NSString * mp3FileName = [self. audioFileSavePath lastPathComponent];
Mp3FileName = [mp3FileName stringByAppendingString: @#];
NSString * mp3FilePath = [self. audioTemporarySavePath stringByAppendingPathComponent: mp3FileName];
@ Try {
Int read, write;
FILE * pcm = fopen ([self. audioFileSavePath cStringUsingEncoding: 1], rb); // location of the audio FILE to which the source is converted
Fseek (pcm, 4*1024, SEEK_CUR); // skip file header
FILE * mp3 = fopen ([mp3FilePath cStringUsingEncoding: 1], wb); // output the location of the generated Mp3 FILE
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 );
}
}