7

I am developing an iPhone app which records the 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];

Above code is recording audio in .wav format. If I want to record audio in MP3 using key kAudioFormatMPEGLayer3(mp3) instead of kAudioFormatLinearPCM(.wav), what other changes do I need to make? Changes in recordSetting dictionary like sample rate, channel and all.

Or suggest any audio format which is compatible with both iPhone and Android, smaller in size and can be recorded directly from iPhone app.

halfer
  • 19,824
  • 17
  • 99
  • 186
ios
  • 6,134
  • 20
  • 71
  • 103

2 Answers2

25

You cannot record in MP3, it is a proprietary format when it comes to encoding. These are the settings I use, it comes out to ~150KB for 2 min 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];

Also doing conversion is a processor intense operation, if you're sending this audio to a server you can use FFMPEG with the mp3lame library to do audio conversion on the server.

EDIT: this is the code for Android recording, it's set to AMR encoding because AAC is only supported as of Honeycomb.

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");
8vius
  • 5,786
  • 14
  • 74
  • 136
  • Thanks for help but how can I get .AAC file from this as .m4a is not supported on android.Please let me know. – ios May 12 '12 at 04:08
  • 1
    My Android developing partner gave me this http://developer.android.com/guide/appendix/media-formats.html there you can see that you can encode in AAC on Android and save a .m4a file so you can have the same .m4a format for both iOS and Android without problem. – 8vius May 14 '12 at 14:19
  • Thanks for your wonderful reply, now i want to know that how can i modulate programmatically in android(ie. change the pitch of the audio) and then convert it in to .m4a format so it works on both.. – ios May 18 '12 at 06:31
  • That seems to be a different topic so I'd suggest opening a new question for that one. As far as the audio recording goes just use the same settings I posted here, as in their Android counterparts and set the filetype to m4a, I'll update my answer with the Android code for this as well. – 8vius May 18 '12 at 14:48
  • use **mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);** for Android. Well tested !! :) Hope it helps someone – Rahul Khurana Aug 05 '17 at 20:50
4

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

Better check out the below link and images:

Audio Recording Reference

enter image description here

Here it is a link to learn the basic for converting the audio format:

Audio Converter Reference

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81