IOS crazy explanation-converting recording audio into Mp3

Source: Internet
Author: User

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 );
}

}
 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.