5

I have decided to break off my very painful relationship with SKAction.playSoundFileNamed and move on to using SKAudioNodes in our project -- the breaking point was their being totally broken by interruptions without any consistency.

We are trying to create an extension to SKAction s.t. it will mimic playing SKAudioNodes without adding delay, similar to when you set the waitForCompletion property in playSoundFileNamed to false. However, we are adding the SKAudioNode and then disposing of it after a short period (3.0 s), but in an extension this would add that 3.0 s delay onto any action. I think we must have this delay and then remove because otherwise we would be accumulating SKAudioNodes unnecessarily. Here is our current code:

func playAudioNode(node: SKSpriteNode?, sound: SoundType, delay: Double)
{
    if node == nil
    {
        return
    }

    let audioNode = audioNodeDictionary[sound]!.copy() as! SKAudioNode

    node!.addChild(audioNode)

    let playSound = SKAction.run
    {
        audioNode.run(.play())
    }

    node!.run(SKAction.sequence([SKAction.wait(forDuration: delay), playSound, SKAction.wait(forDuration: 3.0), SKAction.removeFromParent()]))
}

Does anyone use SKAudioNodes in a similar ad hoc way and use them in SKAction sequences and have any thoughts on best way to implement this?

Mike Pandolfini
  • 532
  • 4
  • 17
  • Just preload your audionodes and they shouldnt delay – Knight0fDragon Feb 12 '19 at 18:52
  • 1
    I wouldn't even bother with this. Keep your audio nodes attached to your sprites that are sound effects, but do not play them. You are most likely going to end up wasting less resources doing it this way. – Knight0fDragon Feb 12 '19 at 18:55
  • 1
    That's what I was thinking initially, actually. Was worried I'd end up with lots of audio nodes all over though, and I didn't have a good sense of memory usage of one of these guys (yet). – Mike Pandolfini Feb 12 '19 at 19:39
  • 2
    The nodes themselves are not intensive. They work off of avplayer, so the audio files loaded in the background should be caches, so it isn’t like you will find the same audio file multiple times in memory – Knight0fDragon Feb 12 '19 at 19:50
  • 1
    Ah -- this is interesting. My sounds are preloaded anyway, so you're saying just use away and don't worry about removal / replacement. – Mike Pandolfini Feb 12 '19 at 20:07
  • 1
    Well keep some caution obviously, but yes, you are better off preloading and keeping assets on. Your memory is a lot cheaper expense than your CPU cycles, so focus more on keeping your program fast. – Knight0fDragon Feb 12 '19 at 20:10

0 Answers0