3

I'm currently playing around with background music and sound effects for my SpriteKit based iOS game. While I managed to set the volume for the background music (using a SKAudioNode) I have some problems with the sound effects. I tried to set the volume for the scene before presenting it (sound effects are played by the scene - here to 0.0 for testing purposes):

run(SKAction.changeVolume(to: 0.0, duration: 0))

But that did not work. And even, if I set the volume to 0.0 before actually playing a sound effect like that:

run(SKAction.sequence([
    SKAction.changeVolume(to: 0.0, duration: 0),
    SKAction.playSoundFileNamed("soundEffect.m4a", waitForCompletion: false)
]))

... it does not work, and you can still hear the sound effect.

What's wrong?

9
  • Try a really really small value instead of 0 for the duration.
    – Sweeper
    Feb 12, 2019 at 8:07
  • Thanks for your quick answer: I tried 0.01 (and 0.5) instead, but it did not work either.
    – Sebastian
    Feb 12, 2019 at 8:54
  • Just noticed that the summary for changeVolume says "Creates an action that changes an audio node’s volume to a new value." - I'm trying to perform that action on a scene node. Maybe that's an issue? However, the action is actually processed. Just added an completion block to check this. But is it really executed?
    – Sebastian
    Feb 12, 2019 at 20:04
  • Yes that’s the issue! The code will still compile if you run it on the scene, but the scene wouldn’t know what to do with it. You should run it on an audio node.
    – Sweeper
    Feb 12, 2019 at 20:27
  • 1
    Hmm, sounds obvious (and works for the SKAudioNode mentioned above playing the background music) - but is it really not possible to set the volume for the scene itself? After all it‘s the scene that‘s playing the sound effect via playSoundFileNamed - if it can play sounds, why can‘t I control the volume?!
    – Sebastian
    Feb 12, 2019 at 20:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.