Rya*_*nes 3 xcode memory-leaks avaudioplayer skaction swift
我的游戏基本上已经完成了。检查 Xcode 中是否存在泄漏。发现每次玩游戏我都会泄漏大约 0.3 MB。然后前往仪器并查看持久数据。
罪魁祸首1:
SKTexture,被多次调用,但并没有持续增长
罪魁祸首2:
希望我记得确切的语法,它类似于 AudioSource,并且在调用此行时发生:SKAction.playSoundFileNamed(...)
所以我进入游戏的“设置”页面并关闭了声音。再次测试内存使用情况,稳定。还意识到 AVAudioPlayer 正在泄漏,因为音效和主题通过不同的机制运行。
我尝试在关闭时将 AVAudioPlayer 的实例设置var backgroundAudio:AVAudioPlayer!为 nil,该实例被声明为类变量,但这没有帮助。
是否有一个巨大的锤子我可以用来清理显然在某些强参考周期下的音频源?当调用 showPhysics 时,我也遇到了 GameViewController 的问题。我认为我的问题可能是游戏的结构。主菜单调用视图和场景,因此场景中还有场景。关卡场景的实例似乎已正确释放,但留下了一些模糊的音频错误。有什么解决办法吗?
您应该使用SKAudioNode实例来避免内存泄漏,SKAction.playSoundFileNamed这个问题已经众所周知了。做这样的事情(在 a 内SKScene):
// Initializing the SKAudioNode instance:
let soundNode = SKAudioNode(fileNamed: "mySound")
audioNode.autoplayLooped = false
// Initializing the audio's duration SKAction:
let soundDuration : TimeInterval = 2 // For a 2 second sound!
let waitToFinish = SKAction.wait(forDuration: soundDuration)
// Adding the SKAudioNode instance to the SKScene:
self.addChild(audioNode)
// Running the actions:
audioNode.run(SKAction.sequence([.play(), waitToFinish]), completion: {
// Removes the SKAudioNode from the SKScene to free the memory:
audioNode.removeFromParent()
})
Run Code Online (Sandbox Code Playgroud)
或者你可以在 Github 上查看我的GameAudioPlayer课程,因为在本例中它完全符合你的要求。它处理SKAudioNodes并避免内存泄漏,您还可以在播放之前加载声音资源: https: //github.com/ThiagoAM/Game-Audio-Player
我希望这对你有帮助!祝你好运!
| 归档时间: |
|
| 查看次数: |
631 次 |
| 最近记录: |