why convert to audio format
Recently in instant messaging, there are always several types of files about voice recording, but you want to interoperate with Android for audio. Then convert the MP3 format audio file or AMR format file. Of course AMR is the best, because the same time period, AMR file data size is the smallest, ideal for mobile device data transmission.
Using Avaudiorecorder in iOS cannot record audio files in MP3 format, although you may have seen such enumerations, but the fact is that only a few types can be generated normally, such as CAF, WAV, and so on.
KAUDIOFORMATLINEARPCM = ' LPCM ', kAudioFormatAC3 = ' ac-3 ', Kaudioformat 60958AC3 = ' Cac3 ', kAudioFormatAppleIMA4 = ' ima4 ', KAUDIOFORMATMPEG4AAC
= ' AAC ', kaudioformatmpeg4celp = ' celp ', kaudioformatmpeg4hvxc = ' hvxc ', KAUDIOFORMATMPEG4TWINVQ = ' Twvq ', kAudioFormatMACE3 = ' MAC3 ', Kaudioformatma CE6 = ' MAC6 ', Kaudioformatulaw = ' Ulaw ', Kaudioformatalaw
= ' Alaw ', kaudioformatqdesign = ' QDMC ', kAudioFormatQDesign2 = ' QDM2 ', Kaudioformatqualcomm = ' QCLP ', KAudioFormatMPEGLayer1 = '. Mp1 ', kaudioformatmpeg Layer2 = '. Mp2 ', KAudioFormatMPEGLayer3 = '. mp3 ', Kaudioformattimecode = ' time ', KaudioFormatmidistream = ' midi ', Kaudioformatparametervaluestream = ' Apvs ', kaudioformatapplelossles s = ' Alac ', kaudioformatmpeg4aac_he = ' Aach ', kaudioformatmpeg4aac_ld = ' AAC L ', Kaudioformatmpeg4aac_eld = ' aace ', kaudioformatmpeg4aac_eld_sbr = ' AACF ', Kaudiofo RMATMPEG4AAC_ELD_V2 = ' AACG ', kaudioformatmpeg4aac_he_v2 = ' AACP ', kaudioformatmpeg4aac_s patial = ' AACs ', kaudioformatamr = ' samr ', KAUDIOFORMATAMR_WB = ' s AWB ', kaudioformataudible = ' audb ', KAUDIOFORMATILBC = ' ILBC ', Kaudio Formatdviintelima = 0x6d730011, kaudioformatmicrosoftgsm = 0x6d730031, kAudioFormatAES3 = ' Aes3 ', kAudioFormatEnhancedAC3 = ' ec-3 '
I am using audio transcoding MP3 Open Source Library lame for conversion. Recording Settings
Audiotoolbox.framework is a C-based framework that uses it to play the sound effect by registering short audio with the system sound service. The System Sound Service is a simple, low-level voice playback service, but it also has some limitations:
Audio playback time cannot exceed 30s
Data must be in PCM or IMA4 format
Audio files must be packaged as one of the. CAF,. AIF,. wav (Note that this is an official document, the actual test found some. mp3 can also be played)
Recorder-(Avaudiorecorder *) Audiorecorder {if (!_audiorecorder) {//Create recording File save path Nsurl *url=[nsurl fileur
Lwithpath:[self Gettheaudiosavepath]];
[Self gettheaudiosavepath];
Create recording format settings Nsdictionary *setting=[self getaudiosetting];
Create a recorder nserror *error=nil;
_audiorecorder=[[avaudiorecorder Alloc]initwithurl:url settings:setting error:&error];
_audiorecorder.delegate=self;
_audiorecorder.meteringenabled=yes;//if you want to monitor sound waves, you must set it to YES [_audiorecorder recordforduration:60.0];
if (Error) {NSLog ("@" errors occurred while creating a Sound Recorder object, error message:%@ ", error.localizeddescription);
[Self chatKeyBoardDidCancelRecording:self.chatKeyBoard];
return nil;
}} return _audiorecorder; }//Record file Save path-(NSString *) Gettheaudiosavepath {nsstring *urlstr=[nssearchpathfordirectoriesindomains (NSDocumentDire
Ctory, Nsuserdomainmask, YES) lastobject]; Urlstr=[urlstr Stringbyappendingpathcomponent:@ "MYRECORD.CAF"];
NSLog (@ "File path:%@", URLSTR);
return urlstr;
}//Audio Recorder Configuration Dictionary-(nsdictionary *) getaudiosetting {nsmutabledictionary *dicm=[nsmutabledictionary dictionary];
Set the recording format [DicM setobject:@ (KAUDIOFORMATLINEARPCM) Forkey:avformatidkey];
Set the recording sampling rate, 8000 is the telephone sampling rate, for the general recording is enough [DicM setobject:@ (8000) Forkey:avsampleratekey];
Set channel, mono/dual Channel [DicM setobject:@ (2) Forkey:avnumberofchannelskey];
sound quality [DicM setobject:@ (Avaudioqualitymin) Forkey:avencoderaudioqualitykey];
Number of bits per sampling point, divided into 8, 16, 24, +//[DicM setobject:@ (8) Forkey:avlinearpcmbitdepthkey];
Whether to use floating-point sampling//[DicM setobject:@ (YES) Forkey:avlinearpcmisfloatkey];
[DicM setobject:@ (NO) Forkey:avlinearpcmisbigendiankey];
[DicM setobject:@12800 forkey:avencoderbitratekey];//decoding rate return DicM; }
convert CAF to MP3 via lame
1. Add Lame library to project
The lame library resource that is added is preferably a 32-bit supported 64-bit LAME library download address: Lame library that supports arm64
2. Import the header file #import "Lame.h"
3. Core code
CAF Asynchronous Turn MP3-(void) conversionaction {dispatch_sync (dispatch_get_global_queue (dispatch_queue_priority_default, 0),
^{nsstring *audiomp3path = [NSString stringwithformat:@ "file://%@", [self Audio_pcmtomp3]];
}); }-(NSString *) Audio_pcmtomp3 {nsstring *urlstr=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDom
Ainmask, YES) lastobject]; NSString *caffilepath =[urlstr stringbyappendingpathcomponent:@ "MYRECORD.CAF"];//original CAF file location nsstring *mp3FilePath = [ur
Lstr stringbyappendingpathcomponent:@ "Myrecord.mp3"];//after conversion MP3 file location @try {int read, write; FILE *PCM = fopen ([Caffilepath 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, 8000);
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 {NSString *urlstr=[nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES)
Lastobject];
NSString *caffilepath =[urlstr stringbyappendingpathcomponent:@ "MYRECORD.CAF"]; NSString *mp3filepath = [urlstr stringbyappendingpathcomponent:@ "Myrecord.mp3"];
NSData *data1= [NSData Datawithcontentsoffile:caffilepath];
NSData *data2= [NSData Datawithcontentsoffile:mp3filepath];
NSLog (@ "%lu,%lu", (unsigned long) data1.length, (unsigned long) data2.length);
return mp3filepath; }
}
4. Note the recording settings of the voice recording are consistent with the setting of the lame conversion, otherwise there will be ambiguity in speech, frequency conversion and so on. The conversion succeeds and will go directly to the @finally code block where you can verify