So I have an AVAudioPlayer, sometimes it works great, but sometimes it prints the error "Error Domain=NSOSStatusErrorDomain Code=1954115647 "(null)"". Here is the code:
override func viewDidLoad() {
super.viewDidLoad()
downloadFileFromURL(url: URL(string: mainPreviewURL)!)
}
func downloadFileFromURL(url: URL) {
var downloadTask = URLSessionDownloadTask()
downloadTask = URLSession.shared.downloadTask(with: url, completionHandler: {
customURL, response, error in
self.play(url: customURL!)
})
downloadTask.resume()
}
func play(url: URL) {
do {
player = try AVAudioPlayer(contentsOf: url)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
player!.prepareToPlay()
player!.play()
} catch {
print(error)
}
}
Make the code in your do statement look like this:
let songData = try NSData(contentsOfURL: songURL!, options: NSDataReadingOptions.mappedIfSafe)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(data: songData!, fileTypeHint: AVFileTypeMpegLayer3)
player!.prepareToPlay()
player!.play()
When you have this kind of error code usually your audio files are not recognized by Xcode / OS or corrupted. To verify this step, you can select one of your audio file and try to playing it inside Xcode:
If you have , instead of this picture, the total absence of your media player like this image:
probably you should fix your audio files. I checked it and I've seen that sometimes it happened when you use versioning operation system like GIT (git merging, pulling from other systems..) or when you made copies of your files to another system. BUT you should remember that High Sierra can play FLAC's audio from the FINDER preview instead of Sierra, so your issue could be related simply to your current OS.
Solution: remove and restore the original audio file/files version. If you try to use it in a simulator running to Sierra you could continue to have this issue, make your tries to High Sierra and see what happened.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With