Reputation: 21
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
scnScene.rootNode.removeActionForKey("bgMusicKey")
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).
self.scnView.scene!.rootNode.removeAllActions()
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.
Upvotes: 1
Views: 732
Reputation: 23
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()
Upvotes: 1
Reputation: 2543
There Is An Easy Answer
self.rootNode.removeAllAudioPlayers()
this would stop all audio players
Upvotes: 0
Reputation: 21
Solved!
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 =)
Upvotes: 1