I want to use SpriteKit
to play sound, the question is that I use the function:
[SKAction playSoundFileNamed:@"sound.wav" waitForCompletion:NO];
But when I want to pause the game, the sound is still play.
How to stop the sound when skview.pause
is set to YES
, and to resume the sound when skview.pause
is set to NO
?
Its been some time, but here is the basis of what you can do using AVAudio. I will use background music in this example but it can be done any way. You could put this in a subclass of SKSpriteNode to have a custom sound that can be pause by itself or others.
//Add this to your imports:
#import <AVFoundation/AVFoundation.h>;
//Add the following variable in your interface.
AVAudioPlayer *player;
// Put this in an init or something of the like.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"musicFileSound" ofType:@"wav"]];
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
Then to start use [_player play];
The use [_player pause]
to pause.
And [_player stop]
Here is the documentation for it, there are some cool things you can do with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With