Recording (PCM format), playback, lame transcoding into MP3

Source: Internet
Author: User
Introduction of the following two libraries. Lame is a third-party library. #import <AVFoundation/AVFoundation.h> #import "Lame.framework/headers/lame.h"
#pragma mark-Audio transcoding/**
* Audio transcoding, from PCM transcoding to MP3
*/
-(void) convertToMP3
{
NSString * Mp3filename = @ "Sendrecord.mp3";
NSString * Documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObjec T];
NSString *mp3filepath = [Documentpath stringbyappendingpathcomponent:mp3filename];
NSString * Audiofilepath = [NSString stringWithFormat: @ "%@/AUDIO.CAF", documentpath];

NSLog (@ "audiopath:%@", Audiofilepath);

@try {
int read, write;

FILE *PCM = fopen ([Audiofilepath cstringusingencoding:1], "RB"); Source converted audio File location
Fseek (PCM, 4 * 1024x768, 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
{
NSLog (@ "MP3 generated successfully:%@", Mp3filepath);
}
}

#pragma mark-Get the recording file save path
/**
* Get recording Original file save path
*
* @return Recording Original file path
*/
-(Nsurl *) getsavepath{
NSString *urlstr=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];
Urlstr=[urlstr stringbyappendingpathcomponent: @ "AUDIO.CAF"];
Nsurl *url=[Nsurl Fileurlwithpath:urlstr];
return URL;
}

-(NSString *) getsavepathtostring{
NSString *urlstr=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObject];
Urlstr=[urlstr stringbyappendingpathcomponent: @ "AUDIO.CAF"];
return urlstr;
}

/**
* Get MP3 File save path
*
* @return MP3 file path
*/
-(Nsurl *) Getmp3path
{
Play MP3
NSString * Mp3filename = @ "Sendrecord.mp3";
NSString * Documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObjec T];
NSString *mp3filepath = [Documentpath stringbyappendingpathcomponent:mp3filename];
return [Nsurl Fileurlwithpath:mp3filepath];
}

-(NSString *) getmp3pathtostring
{
Play MP3
NSString * Mp3filename = @ "Sendrecord.mp3";
NSString * Documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) lastObjec T];
NSString *mp3filepath = [Documentpath stringbyappendingpathcomponent:mp3filename];
return mp3filepath;
}

#pragma mark-Get the recording file set
/**
* Get audio File Settings
*
* @return Recording settings
*/
-(Nsdictionary *) getaudiosetting{
Nsmutabledictionary *dicm=[Nsmutabledictionary Dictionary];
Set the recording format
[DicM setobject: @ (KAUDIOFORMATLINEARPCM) Forkey:avformatidkey];
Set the recording sample rate, 8000 is the phone sample rate, for the general recording is enough
[DicM setobject: @ (11025.0) Forkey:avsampleratekey]

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.