Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause the sound when use SpriteKit

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?

like image 905
Javis Avatar asked Nov 03 '13 15:11

Javis


1 Answers

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.

like image 71
Keely Avatar answered Sep 20 '22 12:09

Keely