Stoping a sound as an action on a node with a key in SceneKit

I've written a function in SceneKit that plays background music as an action on a node with a key so I could stop the music playing using the key

I have a global variable so I can call to stop the action from anywhere in the class, (by the way this is SceneKit not SpriteKit, I am still using Swift as a language though)

var musicPlayingAction = SCNAction()

however nothing stops it, either forKey or removeAllActions as in...

This does nothing


So I try a higher level and drill down, killing all actions either on the scene, the view or the node (depending on where I attach the sound action).


still nothing...

Even if I run the sound action on a specific node and try to kill that.. you guessed it nothing or should I say the music is still playing.

This is the function

func playMusic() {
        if game.state == .Playing {

            let music = SCNAudioSource(fileNamed: "art.scnassets/Sounds/BgMusic.mp3")!

            music.volume = 0.3;
            music.loops = true
            music.shouldStream = true
            music.positional = false
            musicPlayingAction = SCNAction.playAudioSource(music, waitForCompletion: false)
            scnScene.rootNode.runAction(musicPlayingAction, forKey: "bgMusicKey")

The Action works as in the music plays but it will not stop.

Is it even possible to stop an AudioSource as an action, I know you can stop Actions with a Key, although this is the first time I have tried to stop an audio SCNAction (note not SKAction) with a key???

Any ideas appreciated, thanks.


Removed the Action forKey...

Split my game up into scenes, added a game state for each scene, then loaded background music dependant on state & scene.

Thanks for your help, much appreciated =)

Stopping/Pausing a sound that is set to repeat forever in SpriteKit , If you are using SKActions for sound you can give the repeat action a key and remove that action later. Create dont forget to call the remove action on the node To create an action, call the class method for the action you are interested in. Then, configure the action’s properties. Finally, to execute the action, call a node object’s run(_:) method (or a similar method from the SCNActionable protocol) and pass it the action object.

There Is An Easy Answer


this would stop all audio players

SceneKit Audio, What is the correct way of using SceneKit's Audio Engine? is attached to a node it should start playing right away if the node is visible (and stop if it is hidden ). The node’s position locates it within the coordinate system of its parent, as modified by the node’s pivot property. The default position is the zero vector, indicating that the node is placed at the origin of the parent node’s coordinate system. You can animate changes to this property’s value. See Animating SceneKit Content.

if you add sound as an action to node you can remove this action with key , if you add sound as audio player you can remove it using remove a yourNode.removeAllAudioPlayers()

removeAction(forKey:), Parameters. key. A string that uniquely identifies a action. Discussion. If the node is currently running an action that matches the key, SceneKit removes that� The hierarchy of nodes, or scene graph, in a scene defines both the organization of its contents and your ability to present and manipulate those contents using SceneKit. You may create a node hierarchy programmatically using SceneKit, load one from a file created using 3D authoring tools, or combine the two approaches.

Can't stop playing audio, runAction(SCNAction.wait(duration: 1.0)) { Swift.print("self.node. but audio is still playing. Anybody knows how to stop audio playing? SceneKit. Asked 3 years � SceneKit was introduced on OS X on Mountain Lion and last year on iOS in iOS 8. Last year, we added a few new features to SceneKit to make it ready for casual games, like particle systems, physics, force fields, and the integration with SpriteKit, for example, for all the 2D game overlays.

SCNNode Class (SceneKit), A node in the scene graph. Gets the list of keys for which there are actions. AudioPlayers. Gets a list of the currently attached audio players. Camera Stops the specified observer from receiving further notifications of changed values for the� To display 3D content with SceneKit, you create a scene containing a hierarchy of the nodes and attributes that together represent your visual elements. Typically, you build your assets in a 3D visual editor, then assemble them into a scene using Xcode’s SceneKit Scene Editor, ready for SceneKit to render.

Combining the Power of SpriteKit and SceneKit, IntroductionSpriteKit and SceneKit are iOS frameworks designed to make it Music & Audio In this tutorial, you are going to use key-value observing, KVO for short. pauseNode.texture = SKTexture(imageNamed: "Pause Button" ) We run the actions in a repeating sequence on the background node. Actions are a simple way to add interactions in your scenes. An action is launched when its trigger is fired. For instance, you can specify that when the user clicks (or touches) a mesh, an action is executed. To use actions, you have to attach an BABYLON.ActionManager to a mesh or to your scene: mesh.actionManager = new BABYLON.ActionManager