Stop Sound playing on Node in SceneKit


April 2019


185 time


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?.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


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

Hope it can help!