AVAudioPlayer在游戏运行一段时间后返回nil

问题描述 投票:2回答:1

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

每个气球对象都包含一个安全设置它的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不可用”。在控制台中为从那时起产生的每个气球表明我的资源没有找到?

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

是否有任何明显的遗漏可能导致这些错误?

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

ios swift avaudioplayer skemitternode
1个回答
0
投票

不......这不行。您必须将audioPlayer变量实例化为类级别。

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

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

class YourClassName : Type {
var audioPlayer : AVAudioPlayer?

yourMethodForCalling() {

}
© www.soinside.com 2019 - 2024. All rights reserved.