Android录制的aac(.mp3)文件无法在iOS中播放

时间:2015-12-10 12:17:30

标签: android ios swift avplayer android-mediarecorder

您好我正在使用以下代码在Android上录制音频

myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
myRecorder.setAudioSamplingRate(44100);
myRecorder.setAudioEncodingBitRate(256);
myRecorder.setAudioChannels(1);
voiceFileName = getFilename();
myRecorder.setOutputFile(voiceFileName);

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, "test");

    if (!file.exists()) {
        file.mkdirs();
    }
    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");

}

但是aac(.mp3)文件无法在iOS上播放。 修改

let audioFilename = getDocumentsDirectory().stringByAppendingPathComponent("whistle.mp3")
let audioURL = NSURL(fileURLWithPath: audioFilename)
let whistlePlayer:AVAudioplayer = try AVAudioPlayer(contentsOfURL: audioURL)
whistlePlayer.play()

如何在IOS(Swift)中播放此文件?

3 个答案:

答案 0 :(得分:4)

我得到的解决方案只是将扩展名更改为.m4a,它可以在ios上运行

let audioFilename = getDocumentsDirectory().stringByAppendingPathComponent("voice.m4a")
let audioURL = NSURL(fileURLWithPath: audioFilename)
do
 {
   audioPlayer = try AVAudioPlayer(contentsOfURL: audioURL)
   audioPlayer.delegate = self
   audioPlayer.play()
 }
catch{
   print("filenotfound")
}

答案 1 :(得分:1)

myRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);建议你应该写入mp4(m4a)。不确定aac是否可以写入mp3容器

答案 2 :(得分:0)

检查你是否可以在iPhone的Safari中播放该文件。 你能告诉你怎么试着播放那个文件吗?