ios - 游戏运行一段时间后,AVAudioPlayer 返回 nil
<p><p>我目前正在开发一个小游戏,气球从屏幕底部浮起来,玩家必须在它们到达顶部之前弹出它们。</p>
<p>每个气球对象都包含一个安全设置它的 AVAudioPlayer 的方法:</p>
<pre><code>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
}
</code></pre>
<p>然后在每个气球的 init() 方法中运行以下代码:</p>
<pre><code>if let popSound = self.setUpAudioPlayerWithFile("PopSound", type: "wav") {
self.popSound = popSound
}
</code></pre>
<p>在游戏开始几分钟之前,这绝对可以正常工作。此时我开始收到“BALLOON ERROR - AudioPlayer not available”。在控制台中,从那时起产生的每个气球都表明我的资源没有被发现?</p>
<p>此时我的 SKEmitterNodes 也开始返回 nil。 (2016-08-13 18:59:54.527 Stop & Pop *** -: 数据为 NULL)</p>
<p>是否有任何明显的遗漏可能导致这些错误?</p>
<p>希望我提供了足够的信息,感谢您的阅读。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>不...这行不通。您必须将 audioPlayer 变量实例化到 Class Level。</p>
<p>我尝试这样做,在类级别的另一个 ViewController 上实例化了 var audioPlayer。</p>
<p>最好的办法是将此方法本地化到您的 ViewController 中,并将 audioPlayer 放在这里:</p>
<pre><code>class YourClassName : Type {
var audioPlayer : AVAudioPlayer?
yourMethodForCalling() {
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 游戏运行一段时间后,AVAudioPlayer 返回 nil,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38930065/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38930065/
</a>
</p>
页:
[1]