I've been struggling with this error and I can't understand where it is coming from. I'm picking a file from the Files app within my App. It returns a correct URL (file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/sound.mp3).
However, when trying to play it with AVAudioPlayer, it gives me the Error Domain=NSOSStatusErrorDomain Code=-54 "(null)". I suspect this is related to permissions or something, but I cannot find how to overcome it. When adding the file to the assets in the project, it plays fine.
Any clue?
The code I'm using is the following:
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard controller.documentPickerMode == .open, let url = urls.first, url.startAccessingSecurityScopedResource() else {
return
}
defer {
url.stopAccessingSecurityScopedResource()
}
do {
print("Playing \(url)")
try audioPlayer = AVAudioPlayer(contentsOf: url)
print("preparing...")
audioPlayer?.prepareToPlay()
print("playing...")
audioPlayer?.play()
} catch {
print(error)
}
controller.dismiss(animated: true, completion: nil)
}
Moreover, I have added the iCloud Documents entitlements...