简体   繁体   English

迅速-播放声音时出现错误“严重错误:在展开可选值时意外发现nil”

[英]Swift - While playing sound I get error “fatal error: unexpectedly found nil while unwrapping an Optional value”

I am using Xcode 7.0.1 Swift 2 iOS 9. While playing sound I get this error: 我正在使用Xcode 7.0.1 Swift 2 iOS9。在播放声音时出现此错误:

"fatal error: unexpectedly found nil while unwrapping an Optional value" “致命错误:解开可选值时意外发现nil”

错误截图

and this is my code: 这是我的代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    playSound(enumerator![indexPath.item] )
}

func playSound(soundName: String)
{
    let coinSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundName, ofType: "m4a")!)
    do{
        let audioPlayer = try AVAudioPlayer(contentsOfURL:coinSound)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }catch {
        print("Error getting the audio file")
    }
}

The reason this is crashing even though your function is safely checking the validity of it's data is because you are force unwrapping your enumerator object, which crashes before the function is called. 即使您的函数安全地检查了其数据有效性,它也崩溃的原因是因为您强行解包了枚举器对象,该对象在调用函数之前就崩溃了。 You need to check that one safely as well! 您还需要安全地检查一个!

Or, as I just glanced as your code again the sound object is never being created (probably not being found in the bundle or incorrect name) and then when you try to force unwrap that, it could also crash. 或者,就像我再次浏览您的代码一样,声音对象从未创建过(可能未在捆绑软件中找到或名称不正确),然后当您尝试强行打开包装时,它也可能崩溃。 Is your print statement ever printed? 您的印刷声明是否印刷过?

The audioPlayer constant you have delcared in the playSound function gets dealloacted as soon as the playSound function completes. 一旦playSound函数完成,您在playSound函数中查询过的audioPlayer常量就会被释放。

Declare the audioPlayer as a property at the class level and they play the sound. 在类级别上将audioPlayer声明为属性,然后它们播放声音。

Here is the sample code : 这是示例代码:

class ViewController: UIViewController {

var audioPlayer = AVAudioPlayer()

...........

func playSound(soundName: String) { func playSound(soundName:String){

    let coinSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundName, ofType: "wav")!)
    do{
        audioPlayer = try AVAudioPlayer(contentsOfURL:coinSound)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }catch {
        print("Error getting the audio file")
    }
}

NSBundle pathForResource(name: String?, ofType ext: String?) -> String? returns an optional, and you are force unwrapping. 返回一个可选参数,您将强制展开。 Either your path is wrong or your resource is not there. 您的路径错误或资源不存在。

And looking at the image sound name has .m4a extension. 并查看图像声音名称的扩展名为.m4a If you want to provide extension yourself, you can skip ofType and pass nil or separate the extension from the name of the resource and send both parameters. 如果要自己提供扩展名,则可以跳过ofType并传递nil或将扩展名与资源名称分开,然后发送两个参数。

To be safe, you should always check for optionals when you are not sure if it has value or not 为了安全起见,当您不确定可选选项是否有价值时,应始终检查可选选项

let pathComponents = soundName.componentsSeparatedByString(".")
if let filePath = NSBundle.mainBundle().pathForResource(pathComponents[0], ofType: pathComponents[1]) {
    let coinSound = NSURL(fileURLWithPath: filePath)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 快速致命错误:解开Optional值时意外发现nil - swift fatal error: unexpectedly found nil while unwrapping an Optional value Swift致命错误:解开Optional值时意外发现nil - Swift fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:在Swift 3中解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Swift 3 SWIFT-致命错误:解开可选值时意外发现nil - SWIFT - fatal error: unexpectedly found nil while unwrapping an Optional value Swift:致命错误:在展开可选值时意外发现nil - Swift : fatal error: unexpectedly found nil while unwrapping an Optional value Swift-致命错误:解开Optional值时意外发现nil - Swift - Fatal error: unexpectedly found nil while unwrapping an Optional values 当我实现Parse时,我得到“致命错误:在展开可选值时意外发现nil” - When I implement Parse I get “fatal error: unexpectedly found nil while unwrapping an Optional value” 致命错误:在Tableview中展开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in Tableview 致命错误:解开可选值(lldb)时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) 致命错误:展开一个可选值(lldb)时意外发现nil - Fatal error: Unexpectedly found nil while unwrapping an Optional value (lldb)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM