问题描述
使用 SKAudioNode
和 SKAction.play()
播放声音时出现以下错误:
2021-01-23 13:58:10.169108-0800 [AppNameHere][56755:10871021] [aurioc] AURemoteIO.h:323:entry: Unable to join I/O thread to workgroup ((null)): 2
声音确实播放,但第一次播放时它会导致应用程序跳跃/跳过/暂停片刻。所以,我收到了一个伴随着暂停的错误。
这是我的代码,它位于从 didBegin(_ contact: SKPhysicsContact)
//Load the sound
let audio = SKAudioNode(fileNamed: "clink.wav")
//Prevent the sound from looping infinitely
audio.autoplayLooped = false
//Add the sound to the scene
self.addChild(audio)
//Play the sound
let playAction = SKAction.play()
audio.run(playAction)
//Wait for 1 second so the sound can complete,then remove it from the scene
dispatchQueue.main.asyncAfter(deadline: .Now() + 1.0) {
audio.removeFromParent()
}
为什么会出现这个问题,我该如何解决?
感谢您的帮助!
更新:我显然需要“编辑问题以包括所需的行为、特定问题或错误,以及重现问题所需的最短代码。这将帮助其他人回答问题。”
期望的行为:声音播放时应该没有错误,并且不会导致应用暂时跳/跳过。
解决方法
有不同的方法可以让这个工作,如果你想使用 SKAudioNode 那么你可以执行以下操作。
在 GameScene 文件顶部附近,创建一个变量:
var audio = SKAudioNode()
在“didMove(to view: SKView)”中添加以下几行:
audio = SKAudioNode(fileNamed: "clink.wav")
audio.autoplayLooped = false
addChild(audio)
在您的函数中(在您的 didBegin(_ contact: SKPhysicsContact) 中调用),输入以下几行:
let playAction = SKAction.play()
let wait = SKAction.wait(forDuration: 1.0)
let grp = SKAction.group([playAction,wait])
let rfp = SKAction.removeFromParent()
let sequence = SKAction.sequence([grp,rfp])
audio.run(sequence)
通常情况下,您不需要这样的方法,但 SKAction.play() 似乎不适用于 completion: 块,因此我必须包含 1 秒的持续时间(这是您很高兴做的)前)。 那应该可行。
另一种更简单的方法是:
var audio: SKAction?
然后在 didMove(to view) 中放入:
audio = SKAction.playSoundFileNamed("Clink.wav",waitForCompletion: false)
然后在你的函数中输入:
self.run(audio!,completion: {
self.removeFromParent()
})