OSStatus error 2003334207 when using AVAudioPlayer

user2747220 picture user2747220 · Mar 17, 2015 · Viewed 17.3k times · Source

I am trying to play an MP3 file (works when played via VLC/iTunes) when a button is pressed. Here is my code:

     var audioPlayer: AVAudioPlayer!
     @IBAction func playEpisode(sender: AnyObject) {
    println("now playing")
    let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
    let data: CDEpisode = fetchedResultsController.objectAtIndexPath(indexPath!) as! CDEpisode
    var err: NSError?
    let url = NSURL(string: data.localPath)
    println("The url is \(url)")

    audioPlayer = AVAudioPlayer(contentsOfURL: url, error: &err)
    if audioPlayer == nil {
        if let e = err {
            println(e.localizedDescription)
        }
    }
    audioPlayer.delegate = self
    audioPlayer.prepareToPlay()
    audioPlayer.play()
}

Here is the log:

now playing
The url is Optional(file:///var/mobile/Containers/Data/Application/4747A71E-A63F-4EFC-B2DF-8B361361080B/Documents/serial-s01-e12.mp3)
The operation couldn’t be completed. (OSStatus error 2003334207.)
fatal error: unexpectedly found nil while unwrapping an Optional value

The EXC_BREAKPOINT happens on the audioPlayer.delegate = self.

Other threads on StackoOverflow do not help. Any ideas? Thanks

Edit: I have tried passing a localURL to contentsOfURL (instead of a CDEpisode object) and it still fails.

Answer

Dima Gimburg picture Dima Gimburg · Mar 19, 2019

This is probably caused by trying to load a file that doesn't exist. If that helps someone adding the call to url.checkResourceIsReachable() will log more descriptive message.

Example code:

    do {
        let url = URL(fileURLWithPath: dbObject.path)
        let isReachable = try url.checkResourceIsReachable()
        // ... you can set breaking points after that line, and if you stopped at them it means file exist.
    } catch let e {
        print("couldnt load file \(e.localizedDescription)")
    }