Audio format for iOS and Android

Sorry if I am asking too generic question.

We are in the process of developing an application which records and plays audio on mobile devices. This application is being developed for both Android and iOS. The application will record audio using device mic and store it in the server.

User (on Android and iOS) can open the application and play the sound which is stored on the server. The sound format we are using is AAC. In iOS its working fine. We can record and play the AAC files.

But on Android (Samsung S3 and Samsung Galaxy Y) we cannot record sound in AAC format. But in S3 we can play the AAC file.

My question is, which format we should select for recording and playing in Android (should support from 2.3 to Jellybean) and iOS. Should we use MP4?

One solution we have is, on the backend side we can convert the audio file to AAC, MP4 or 3GP and give the supported files to mobiles based on the versions.


Solution 1:

Android and iOS use the same server to storage records. All records are saved without any extension.

When Android downloads a record from the server, my app adds an ".aac" extension.
When using iOS, it adds an ".m4a" extension.

The same record plays good on both mobile platforms.

Record on iOS:

NSDictionary recorderSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                        [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
                        [NSNumber numberWithInt:16000],AVSampleRateKey,
                        [NSNumber numberWithInt:1],AVNumberOfChannelsKey,
                        [NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
                        [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                        [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                        nil];

For Android:

myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
myRecorder.setAudioSamplingRate(16000);
myRecorder.setAudioChannels(1);
mRecorder.setOutputFile(fileName);

Solution 2:

We recently completed one app with this kind of functionality recording from both and listening from both iOS and Android, below is code we used for your reference.We relied on our webservice and server for conversion to .mp3 format which can be easily played in both platform.

//iPhone side code. records m4a file format (small in size)

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                [NSNumber numberWithFloat:16000.0], AVSampleRateKey,
                                [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error];

if (error)
    NSLog(@"error: %@", [error localizedDescription]);
else
    [audioRecorder prepareToRecord];


//Android side code. (records mp4 format and it works straight away by giving mp3 extension.)
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(256);
recorder.setAudioChannels(1);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(getFilename());

try {
    recorder.prepare();
    recorder.start();
    myCount=new MyCount(thirtysec, onesecond);
    myCount.start();
    recordingDone=true;
    count=0;
    handler.sendEmptyMessage(0);
    if(progressDialog.isShowing())
        progressDialog.dismiss();
} catch (IllegalStateException e) {
    e.printStackTrace();
    StringWriter strTrace = new StringWriter();
    e.printStackTrace(new PrintWriter(strTrace));
    CrashReportActivity.appendLog(
                                  "\nEXCEPTION : \n" + strTrace.toString() + "\n",
                                  Comment.this);
} catch (IOException e) {
    e.printStackTrace();
    StringWriter strTrace = new StringWriter();
    e.printStackTrace(new PrintWriter(strTrace));
    CrashReportActivity.appendLog(
                                  "\nEXCEPTION : \n" + strTrace.toString() + "\n",
                                  Comment.this);
}

You can find out conversion code from m4a to mp3 in .Net easily, look around by googling (lame or faad some utility like that).

Hope this helps.

Solution 3:

We recently faced this interoperability issue. We were trying to record a file in aac format in both platforms but the files recorded using Samsung handsets were not played in iOS. So we had to use both 3gp as well as aac formats in order to play the file across multiple devices in android and ios.

Following is the code that we used when recording a file in Android:

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mRecorder.setOutputFile(fileName);

We kept the extension of the recorded file as .3gp.

In iOS, however the recording can not be performed in 3gp so we recorded file in .aac in iOS which can be played in both the platforms.