简体   繁体   English

适用于 iOS 和 Android 的音频格式

[英]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.此应用程序正在为 Android 和 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.用户(在 Android 和 iOS 上)可以打开应用程序并播放存储在服务器上的声音。 The sound format we are using is AAC.我们使用的声音格式是 AAC。 In iOS its working fine.在 iOS 中它工作正常。 We can record and play the AAC files.我们可以录制和播放 AAC 文件。

But on Android (Samsung S3 and Samsung Galaxy Y) we cannot record sound in AAC format.但在 Android(三星 S3 和三星 Galaxy Y)上,我们无法以 AAC 格式录制声音。 But in S3 we can play the AAC file.但是在 S3 中我们可以播放 AAC 文件。

My question is, which format we should select for recording and playing in Android (should support from 2.3 to Jellybean) and iOS.我的问题是,我们应该选择哪种格式在Android(应该支持从2.3到Jellybean)和iOS中进行录制和播放。 Should we use MP4?我们应该使用 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.我们有一个解决方案,在后端,我们可以将音频文件转换为 AAC、MP4 或 3GP,并根据版本将支持的文件提供给手机。

Android and iOS use the same server to storage records. Android 和 iOS 使用相同的服务器来存储记录。 All records are saved without any extension.所有记录都被保存,没有任何扩展名。

When Android downloads a record from the server, my app adds an " .aac " extension.Android从服务器下载记录时,我的应用程序添加了一个“ .aac ”扩展名。
When using iOS , it adds an " .m4a " extension.使用iOS 时,它会添加“ .m4a ”扩展名。

The same record plays good on both mobile platforms.相同的记录在两个移动平台上都表现良好。

Record on iOS:在 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);

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.我们最近完成了一个具有这种功能的应用程序,可以同时从 iOS 和 Android 进行录音和收听,以下是我们使用的代码供您参考。我们依靠我们的网络服务和服务器转换为 .mp3 格式,可以轻松地在这两种格式中播放平台。

//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).您可以轻松地在 .Net 中找到从 m4a 到 mp3 的转换代码,通过谷歌搜索(跛脚或 faad 一些类似的实用程序)四处看看。

Hope this helps.希望这可以帮助。

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.我们试图在两个平台上以 aac 格式录制文件,但使用三星手机录制的文件无法在 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.因此,我们必须同时使用 3gp 和 aac 格式才能在 android 和 ios 中的多个设备上播放文件。

Following is the code that we used when recording a file in Android:以下是我们在 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.我们将录制文件的扩展名保留为 .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.但是在iOS中无法进行3gp录制,所以我们在iOS中录制了.aac格式的文件,可以在两个平台上播放。

With MPEG4 and AAC combination, all audio plays on Android.使用 MPEG4 和 AAC 组合,所有音频都可以在 Android 上播放。 But on iOS, we had to use "m4a" as the file extension so that Android audio plays.但是在 iOS 上,我们必须使用“m4a”作为文件扩展名,以便 Android 音频播放。

Programatically this will help you and the file extension may mp3/mp4 for audio;以编程方式,这将对您有所帮助,文件扩展名可能是 mp3/mp4 音频; both will support on media players.两者都将支持媒体播放器。 Android snippet:安卓片段:

myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

iOS snippet iOS 代码段

AVFormatIDKey: @(kAudioFormatMPEG4AAC)

Try this, i was having the same issue it's work perfectly.试试这个,我遇到了同样的问题,它完美地工作。

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

Android uses output format MPEG 4 and audio encoder AAC Android 使用输出格式MPEG 4和音频编码器AAC

iOS uses kAudioFormatMPEG iOS 使用kAudioFormatMPEG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM