6

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?

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Javis
  • 79
  • 1
  • 4
  • If I'm not wrong you should use the playSoundFileNamed method for playing sound effects... something short 1 or 2 seconds, like explosion sound – lionserdar Nov 03 '13 at 19:09
  • And also I think it would be better if you use AVAudioPlayer for background music, check this tutorial on raywenderlich.com(http://www.raywenderlich.com/49625/sprite-kit-tutorial-space-shooter) see the sound section – lionserdar Nov 03 '13 at 19:12
  • you could also use an audio engine like ObjectAL – CodeSmile Nov 03 '13 at 21:47
  • i use the playSoundFileNamed method to play some short sounds, just like explosion,but it should be pause; – Javis Nov 04 '13 at 05:56

1 Answers1

9

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.

Keely
  • 366
  • 1
  • 4
  • 19