无法使用 AVAudioPlayer 播放存储在文档目录中的本地 mp3 文件
Unable to play local mp3 file stored in documents directory using AVAudioPlayer
我有一个在三星 android 设备上生成的 mp3 音频文件。我从服务器下载同一个文件并存储在文档目录中。现在我使用下面的代码来播放这个音频文件。 iAudioFileURL 是我在外部用来设置音频文件的属性 url。设置 url 后,我使用下面的代码 url 为 url 设置了 AVAudioPlayer
var iAudioFileURL : URL?{
didSet{
if let audioFileURL = iAudioFileURL {
if audioFileURL.isFileURL {
if let previousURL = iAVAudioPlayer?.url {
if previousURL.path == audioFileURL.path {
return
}
}
self.stopPlayer()
self.iToolBar.items?[0] = iPlayButton
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [AVAudioSessionCategoryOptions.mixWithOthers, AVAudioSessionCategoryOptions.duckOthers])
try AVAudioSession.sharedInstance().setActive(true)
try iAVAudioPlayer = AVAudioPlayer(contentsOf: audioFileURL)
} catch let error as NSError {
iError = error
return
}
iError = nil
iAVAudioPlayer?.prepareToPlay()
self.iProgressSlider.minimumValue = 0
if let duration = self.iAVAudioPlayer?.duration {
let durationNumber = NSNumber(floatLiteral: duration)
self.iProgressSlider.maximumValue = durationNumber.floatValue
}
} else {
print("Path:\(iAudioFileURL?.path) is not a file url")
}
}
}
}
该代码对于其他文件工作正常,但对于从该设备生成的所有 mp3 文件无法播放。
我有一个按钮可以触发 AVAudioPlayer 实例的播放方法。
我已经从 Whosebug 引用了大部分问题和答案。他们没有为我的案子提供解决方案。即使是方法也不会抛出错误。请帮助我 out.You 可以找到文件 here
问题是即使扩展名是 mp3,文件的 mimetype 也是 audio/x-hx-aac-adts。在此线程中 [https://discussions.apple.com/thread/2365009?tstart=0][1] varjak paw 回复
iTunes, to the best of my knowledge, does not support AAC in ADTS
(Audio Data Transport Stream), only AAC in MPEG-4. You'll need to find
a converter to get these files into MPEG-4 before iTunes will be able
to read them. Sorry, I can't offer any suggestions as to a converter
to try.
Regards.
所以这清楚地表明不支持 mimetype audio/x-hx-aac-adts 的文件。
您可以在 mac 上打开终端并使用下面的命令获取文件的 mimetypes
file --mime-type -b myfile.extension
我有一个在三星 android 设备上生成的 mp3 音频文件。我从服务器下载同一个文件并存储在文档目录中。现在我使用下面的代码来播放这个音频文件。 iAudioFileURL 是我在外部用来设置音频文件的属性 url。设置 url 后,我使用下面的代码 url 为 url 设置了 AVAudioPlayer
var iAudioFileURL : URL?{
didSet{
if let audioFileURL = iAudioFileURL {
if audioFileURL.isFileURL {
if let previousURL = iAVAudioPlayer?.url {
if previousURL.path == audioFileURL.path {
return
}
}
self.stopPlayer()
self.iToolBar.items?[0] = iPlayButton
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [AVAudioSessionCategoryOptions.mixWithOthers, AVAudioSessionCategoryOptions.duckOthers])
try AVAudioSession.sharedInstance().setActive(true)
try iAVAudioPlayer = AVAudioPlayer(contentsOf: audioFileURL)
} catch let error as NSError {
iError = error
return
}
iError = nil
iAVAudioPlayer?.prepareToPlay()
self.iProgressSlider.minimumValue = 0
if let duration = self.iAVAudioPlayer?.duration {
let durationNumber = NSNumber(floatLiteral: duration)
self.iProgressSlider.maximumValue = durationNumber.floatValue
}
} else {
print("Path:\(iAudioFileURL?.path) is not a file url")
}
}
}
}
该代码对于其他文件工作正常,但对于从该设备生成的所有 mp3 文件无法播放。 我有一个按钮可以触发 AVAudioPlayer 实例的播放方法。 我已经从 Whosebug 引用了大部分问题和答案。他们没有为我的案子提供解决方案。即使是方法也不会抛出错误。请帮助我 out.You 可以找到文件 here
问题是即使扩展名是 mp3,文件的 mimetype 也是 audio/x-hx-aac-adts。在此线程中 [https://discussions.apple.com/thread/2365009?tstart=0][1] varjak paw 回复
iTunes, to the best of my knowledge, does not support AAC in ADTS (Audio Data Transport Stream), only AAC in MPEG-4. You'll need to find a converter to get these files into MPEG-4 before iTunes will be able to read them. Sorry, I can't offer any suggestions as to a converter to try.
Regards.
所以这清楚地表明不支持 mimetype audio/x-hx-aac-adts 的文件。 您可以在 mac 上打开终端并使用下面的命令获取文件的 mimetypes
file --mime-type -b myfile.extension