I have a tableViewCell and i want to play a sound typically .m4a inside it.
I have Globally declared my AVAudioPlayer
instance like this:
var player = AVAudioPlayer()
Then I download my sound, put it in a directory in Documents. But, when i try to play my sound file from the directory i get this error:
Open failed
The operation couldn’t be completed. (OSStatus error 2003334207.)
This is my code I don't you what is wrong. and I tracked down the files in the simulator I could play them with 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()
}