IOS converts the recorded audio to MP3 through (lame)

Source: Internet
Author: User

Reprinted, please indicate the source, retain the original post address and the author's signature.

URL: http://blog.csdn.net/ysy441088327/article/details/7392842#reply

Author: Xu Shu

In order to enable audio communication between iPhone and Android, audio files in MP3 format are no better.

It is best to convert the data to AMR in 10 seconds.AmrThe file size is about 5 kb. It is suitable for data transmission on mobile devices.

It is mainly used hereLame, A greatMP3Audio Encoder.

So before conversion? You need to record the audio file and set the following parameters before recording the audio using avaudiorecorder:

Nsstring * recordtemporarypathstring = [nsstring stringwithformat: @ "% @/temporary", self. audiotemporarysavepath]; // linearpcm is a lossless coding format for iOS, but it is bulky. // set nsmutabledictionary * recordsettionary = [[nsmutabledictionary alloc] init] for recording. // The recording format cannot use [recordsetemedisetvalue: [nsnumber numberwithint: Encrypted] forkey: avformatidkey]; // sampling rate [recordsetemedisetvalue: [nsnumber numberwithfloat: 11025.0] forkey: Encrypted]; // 44100.0 // number of channels [recordset=setvalue: [nsnumber numberwithint: 2] forkey: avnumberofchannelskey]; // Number of linear sampling digits // [recordset=setvalue: [nsnumber numberwithint: 16] forkey: avlinearpcmbitdepthkey]; // audio quality, sampling quality [recordset1_setvalue: [nsnumber numberwithint: avaudioqualitymin] forkey: avencoderaudioqualitykey];

The volume of the audio file recorded using the above parameters is very large, but don't worry. This is only the first step. After the file is successfully converted to MP3, it can ensure that the volume of the file is around 4 kb per second. ^

In addition, unless you have too much time, you do not need to set other types of parameters for conversion, because the author has tried a lot and it is only guaranteed by the above parameters, sound quality is complete and smooth.

The following describes how to use the lame static library. There are two core files:

1: The lame library is added to the project. Note: other static libraries can be added directly to the static library.

2: Introduce the header file lame. h

#include "lame.h"

3: Core conversion code

-(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"); // location of the audio file converted by source fseek (PCM, 4*1024, seek_cur );// Skip file header file * MP3 = fopen ([mp3filepath cstringusingencoding: 1], "WB"); // position of the MP3 file generated by the output is 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 (); Round (lame, 11025.0); Round (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 = Merge (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 );}}

NOTE: If there is no accident, after the conversion is successful, it will directly go to the @ finally code block.

InReal MachineAndSimulatorIt can be converted perfectly. It is worth noting that the simulator occasionally produces noises when recording audio, so the MP3 after conversion will also be accompanied by noises and I don't know why, I have not encountered it on a real machine yet.

The conversion duration is determined by the size of the recorded audio file. Therefore, we recommend that you enable a new thread to process this task. The Code is as follows:

[NSThread detachNewThreadSelector:@selector(audio_PCMtoMP3) toTarget:self withObject:nil];

The file suffix can also determine the format of the generated recording audio, for example,. wav. caf.



At least it should end. Upload the lame static library. If you need it, download it.
Click the password here: is9i9w

In this blog post, I modified it again. I deleted some wrong notes and re-tested and tried the code. Basically, it was okay.



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.