嗨,我正在使用以下代码在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)中播放此文件?
最佳答案
我得到的解决方案只是将扩展名更改为.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")
}