Question

I'm currently developing a game using Apple's SpriteKit library but I'm unable to stop a sound after is has began playing. I have tried using the listed methods from the documentation in order to stop the Skaction from running but nothing seems to stop the sound from playing. These have been my attempts of trying to stop the sound.

//Playing sound action:

SKAction *music = [SKAction playSoundFileNamed:@"menu.wav" waitForCompletion:YES];
    [musicNode runAction:[SKAction repeatActionForever:[SKAction sequence:@[
                                                    music,
                                                    [SKAction waitForDuration:0],
                                                    ]]] withKey:@"MUSIC_PLAYING"];

//Later when i want to stop the sound:

[musicNode removeActionForKey:@"MUSIC_PLAYING"]; //Removing the action from the node
[musicNode runAction:[SKAction waitForDuration:0] withKey:@"MUSIC_PLAYING"]; //"Overriding" with another action with the same key
[musicNode removeAllActions]; //Removing all actions from the node
[musicNode removeFromParent]; //Removing the node from the parent

The musicNode is simply an SkpriteNode that runs the action, though I tried the same methods directly from the scene i.e: The scene plays the sound and later attempts to remove it. Or maybe the action will not stop because of the waitForCompletion set to YES, but then again, I tried with NO and same results. Could there be someway of using AVaudioSession's sharedInstante to stop the sound? I also tried the following method but also failed:

 [[AVAudioSession sharedInstance]setActive:FALSE error:nil];

Thanks for any help!

Was it helpful?

Solution

Ive had similar issues using SK audio.. and as a result I have fallen back on using AVFoundation. I found this a while ago; it does everything I need it to do and sounds like it might fix your issue as well!

https://github.com/nicklockwood/SoundManager

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top