这是我的代码示例...对于少数音频文件(mp4扩展名),它失败,异常“操作无法完成。(OSStatus error 2003334207.)” 我从iPhone下载了容器并确认文件存在于路径
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
//guard let player = player else { return }
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
audioPlayer.play()
audioPlayer.volume = 1.0
if timer != nil && timer.isValid {
timer.invalidate()
timer = nil
}
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(LynkAudioPlayer.checkPlayTime), userInfo: nil, repeats: true)
} catch let error as NSError {
print(error.description)
}
答案 0 :(得分:0)
您的代码看起来很好。发生此错误的原因是您要么访问不在指定url
的文件,要么在返回后返回值nil
。确保您使用AVAudioPlayer(contentsOf: url)
访问的资源具有正确形成的URL。音频数据也必须采用Core Audio支持的格式。设置后,您可能需要检查audioPlayer
是否为nil
:
audioPlayer = try AVAudioPlayer(contentsOf: url)
if audioPlayer != nil {
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
audioPlayer.play()
audioPlayer.volume = 1.0
} else {
//try to load a different resource?
}