How to pause sound when using SpriteKit

I want to use SpriteKit to play sound, the question is, what am I using the function:

 [SKAction playSoundFileNamed:@"sound.wav" waitForCompletion:NO]; 

But when I want to pause the game, the sound is still playing.

How to stop the sound if skview.pause set to YES and resume the sound if skview.pause set to NO ?

+6
source share
1 answer

It has been a while, but here is the foundation of what you can do using AVAudio. In this example, I will use background music, but this can be done in any way. You can subclass it in SKSpriteNode to have your own sound, which can be paused by itself or by 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 using [_player play];

Use [_player pause] to pause.

And [_player stop]

Here is the documentation for him, there are some interesting things you can do with him.

+9
source

Source: https://habr.com/ru/post/957290/


All Articles