Stop Sound playing on Node in SceneKit

Refresh

April 2019

Views

185 time

1

I have the following code for a gesture recogniser that plays a horn, when tapped, and stops when it is released:

@objc func didPressHorn(_ sender: UILongPressGestureRecognizer) {

    let tapLocation = sender.location(in: sceneView)

    if sender.state == .began {

        if (wasNodeTapped(node: hornNode!, tapLocation: tapLocation)) {
            hornNode!.runAction(SCNAction.playAudio(hornSound, waitForCompletion: false), forKey: "Horn")
            hornPlaying = true
        }

    } else if sender.state == .ended {
        if hornPlaying {
            // tried all the possible solutions, but the sound won't stop
            hornNode?.removeAction(forKey: "Horn")
            hornNode?.removeAllActions()
            hornNode?.isPaused = true
            hornSound.volume = 0
            hornPlaying = false
            print("Horn off") //gets printed!
        }
    }
}

As you can see, I'm trying all possible solutions (that I know), and also the print statements gets printed. However, the sound will not switch off. Do you have an idea, why?

1 answers

3

I don't know if its the best idea/pratice, but you could try: self. hornNode.removeAllAudioPlayers()

Hope it can help!