我有一个tableViewCell,我想在其中播放典型的.m4a声音。 我已经像这样全局声明了我的AVAudioPlayer
实例:
var player = AVAudioPlayer()
然后我下载了我的音频文件,并将其放置在“文档”目录下的一个文件夹中。但是,当我尝试从该文件夹播放我的音频文件时,出现以下错误:
Open failed
The operation couldn’t be completed. (OSStatus error 2003334207.)
这是我的代码,我不知道哪里有问题。我在模拟器中找到了这些文件,可以用QuickTimePlayer播放。
func downloadSound(_ message: ChatVoiceMessageStructure) {
guard let url = service?.networkManager.CDNURL else { return }
let doanloadURL = URL(string: url.appending(message.name!))
let docUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
let desURL = docUrl.appendingPathComponent("tmpsong.m4a")
var downloadTask:URLSessionDownloadTask
let request = URLRequest(url: doanloadURL!)
downloadTask = URLSession.shared.downloadTask(with: request, completionHandler: { [weak self](URLData, response, error) -> Void in
do{
let isFileFound:Bool? = FileManager.default.fileExists(atPath: desURL.path)
if isFileFound == true{
print(desURL) //delete tmpsong.m4a & copy
} else {
try FileManager.default.copyItem(at: URLData!, to: desURL)
}
let sPlayer = try AVAudioPlayer(contentsOf: desURL)
self?.player = sPlayer
self?.player.prepareToPlay()
self?.player.play()
} catch let err {
print(err.localizedDescription)
}
})
downloadTask.resume()
}