iOS crazy detailed recording audio converted into MP3

Source: Internet
Author: User

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

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.