OSStatus error 2003334207 when using AVAudioPlayer

前端 未结 7 1231
野的像风
野的像风 2021-01-17 13:10

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!
     @IBAc         


        
相关标签:
7条回答
  • 2021-01-17 13:25

    As others have said, the audio player couldn't find the file.

    This helped me: Document directory path change when rebuild application

    Basically, you cannot load files with an absolute reference, as the sandbox environment regenerates the absolute file url each time. So you will need to add a small bit of code (see above) to get the correct urls to use.

    0 讨论(0)
  • 2021-01-17 13:28

    I also got this problem, after checking up the audio file url, found that it is stored in Cache directory. so audio player probably couldn't find audio file according to your "url".

    please ensure, the url path is under Document directory.

    0 讨论(0)
  • 2021-01-17 13:28

    try this one

    var player: AVAudioPlayer = AVAudioPlayer()
    
    
    @IBAction func playX(_ sender: Any) {
        let urlstring = "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3"
        let url = URL(string: urlstring)
        let data = try! Data(contentsOf: url!)
        player = try! AVAudioPlayer(data: data)
        player.prepareToPlay()
        player.volume = 1.0
        player.play()
    }
    
    0 讨论(0)
  • 2021-01-17 13:29

    It looks like your trying to unwrap a variable that has a nil value. You should safely unwrap your variables to prevent this.

    if let data: CDEpisode = fetchedResultsController.objectAtIndexPath(indexPath!) as! CDEpisode
    {
        var err: NSError?
        let url = NSURL(string: data.localPath)
        println("The url is \(url)")
    
        //rest of code
    }
    

    You will still need to figure out why it is returning nil but this is a safer way to unwrap variables and prevent crashing as there would need to be more context to resolve that issue.

    Some questions to look into:

    • Are you sure the fetchedResultsController is returning an object at all?
    • Are you sure it is of CDEpisode?
    0 讨论(0)
  • 2021-01-17 13:30

    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)")
        }
    
    0 讨论(0)
  • 2021-01-17 13:39

    You're checking if audioPlayer is nil but then you go on to use it as if it wasn't anyway. You probably want something like:

    if audioPlayer == nil {
        if let e = err {
            println(e.localizedDescription)
        }
    } else {
        audioPlayer.delegate = self
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }
    

    And do something to actually handle the error case rather than just printing the error.

    0 讨论(0)
提交回复
热议问题