【问题标题】:AVAudioPlayer returning nil after game has been running for a while游戏运行一段时间后,AVAudioPlayer 返回 nil
【发布时间】:2016-08-13 06:52:25
【问题描述】:

我目前正在开发一个小游戏,气球从屏幕底部浮起来,玩家必须在它们到达顶部之前将它们弹出。

每个气球对象都包含一个安全设置它的 AVAudioPlayer 的方法:

func setUpAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? {

    let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
    let url = NSURL.fileURLWithPath(path!)

    var audioPlayer:AVAudioPlayer?

    do {
        try audioPlayer = AVAudioPlayer(contentsOfURL: url)
    }catch {
        print("BALLOON ERROR - AudioPlayer not avaliable.")
    }

    return audioPlayer
}

然后在每个气球的 init() 方法中运行以下代码:

if let popSound = self.setUpAudioPlayerWithFile("PopSound", type: "wav") {
        self.popSound = popSound
    }

在游戏开始几分钟之前,这绝对可以正常工作。此时我开始收到“BALLOON ERROR - AudioPlayer not available”。在控制台中,从那时起产生的每个气球都表明我的资源没有被发现?

此时我的 SKEmitterNodes 也开始返回 nil。 (2016-08-13 18:59:54.527 Stop & Pop[256:13785] *** -[NSKeyedUnarchiver initForReadingWithData:]: 数据为 NULL)

有什么明显的我遗漏的东西可能导致这些错误吗?

希望我已经提供了足够的信息,感谢您的阅读。

【问题讨论】:

    标签: ios swift avaudioplayer skemitternode


    【解决方案1】:

    不...这行不通。您必须将 audioPlayer 变量实例化到 Class Level。

    我尝试这样做,在类级别的另一个 ViewController 上实例化了 var audioPlayer。

    最好的办法是将此方法本地化到您的 ViewController 中,并将 audioPlayer 放在这里:

    class YourClassName : Type {
    var audioPlayer : AVAudioPlayer?
    
    yourMethodForCalling() {
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      相关资源
      最近更新 更多