IPhone App: record audio to mp3

I am developing an iPhone application that records audio in .wav format.

here is the code

NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc]init]; [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey]; [recordSetting setValue:[NSNumber numberWithFloat:11025.0] forKey:AVSampleRateKey]; [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey]; [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [path objectAtIndex:0]; NSString *myDBnew = [documentsDirectory stringByAppendingPathComponent:@"test.wav"]; recordedTmpFile = [NSURL fileURLWithPath:myDBnew]; NSLog(@"Using File called: %@",recordedTmpFile); recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error]; [recorder setDelegate:self]; [recorder prepareToRecord]; [recorder record]; 

The audio in .wav format is recorded above the code. If I want to record audio to MP3 using the kAudioFormatMPEGLayer3 (mp3) key instead of kAudioFormatLinearPCM (.wav), what other changes do I need to make? changes to the recordSetting dictionary, such as sample rate, channel and all

or Offer any audio format that is compatible with iPhone and Android, smaller and can be recorded directly from the iPhone App

Please help and suggest.

Thanks.

+6
source share
2 answers

You cannot record to MP3, it is a proprietary format when it comes to encoding. These are the settings that I use, it reaches ~ 150 KB in 2 minutes of recording time:

 NSString *tempDir = NSTemporaryDirectory(); NSString *soundFilePath = [tempDir stringByAppendingPathComponent:@"sound.m4a"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, [NSNumber numberWithFloat:8000.0], AVSampleRateKey, [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey, nil]; 

In addition, conversion is processor intensive work, if you send this audio to a server, you can use FFMPEG with the mp3lame library to convert sound to the server.

EDIT: This is the code for writing to Android, it is set to AMR encoding, because AAC is only supported as a cellular.

 mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setAudioChannels(1); mediaRecorder.setOutputFile("sample.m4a"); 
+23
source

You cannot record audio in .mp3 format. kAudioFormatMPEGLayer3 is for playback the .mp3 format audio . There is no codec available for .mp3 recording . You need to burn in aac or wav format and convert it to .mp3 format.

Better check out the link below and the images:

Link to audio recording

enter image description here

Here is a link to learn the basics for audio format conversion:

Link to Audio Converter

+1
source

Source: https://habr.com/ru/post/915489/


All Articles