使用AVAudioPlayer时出现OSStatus错误2003334207 [英] OSStatus error 2003334207 when using AVAudioPlayer

查看:124
本文介绍了使用AVAudioPlayer时出现OSStatus错误2003334207的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按下按钮时播放MP3文件(通过VLC/iTunes播放时可以工作).这是我的代码:

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()
}

这是日志:

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

EXC_BREAKPOINT发生在audioPlayer.delegate = self上.

StackoOverflow上的其他线程无济于事.有任何想法吗? 谢谢

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

编辑:我尝试将localURL传递到contentsOfURL(而不是CDEpisode对象),但仍然失败.

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

推荐答案

似乎您试图解开具有nil值的变量.您应该安全地解开变量以防止这种情况.

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
}

您仍然需要弄清楚为什么它返回nil,但这是解开变量并防止崩溃的更安全的方法,因为需要更多的上下文来解决该问题.

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.

要研究的一些问题:

  • 您确定fetchedResultsController正在返回一个对象吗? 全部?
  • 您确定它是CDEpisode的吗?
  • Are you sure the fetchedResultsController is returning an object at all?
  • Are you sure it is of CDEpisode?

这篇关于使用AVAudioPlayer时出现OSStatus错误2003334207的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆