Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

swift - Why is my SKAudioNode giving me an error?

I'm getting the following error when playing a sound using SKAudioNode and 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

The sound does play, but the very first time it plays it causes the app to sort of jump/skip/pause for a moment. So, I'm getting an error accompanied by a pause.

Here's my code, which sits in a function that's called from 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()
}

Why is this problem happening, and how do I solve it?

Thanks for your help!

UPDATE: I apparently need to "Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question."

Desired behavior: The sound should play without an error and without causing the app to jump/skip for a moment.

Specific problem or error: See error above.

Shortest code necessary to reproduce the problem: See code above.

I've also added notes in the code to explain what each line does.

question from:https://stackoverflow.com/questions/65864959/why-is-my-skaudionode-giving-me-an-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are different ways to get this working, if you want to use SKAudioNode then you could do the following.

Near the top of the GameScene file, create a variable:

var audio = SKAudioNode()

In "didMove(to view: SKView)" add the below lines:

audio = SKAudioNode(fileNamed: "clink.wav")
audio.autoplayLooped = false
addChild(audio)

In your function (which gets called in your didBegin(_ contact: SKPhysicsContact)), put the below lines in:

  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)

Typically, you wouldn't need such an approach, but SKAction.play() doesn't seem to work with the completion: block, therefore i had to include a duration of 1 second (which is what you were happy doing before). That should work.

Another approach, which is simpler, is:

var audio: SKAction?

Then in didMove(to view) put:

audio = SKAction.playSoundFileNamed("Clink.wav", waitForCompletion: false)

Then in your function just put this:

  self.run(audio!, completion: {
    self.removeFromParent()
    })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...