How to pause the sound when use SpriteKit

蓝咒 提交于 2019-12-19 17:45:34

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/19754871/how-to-pause-the-sound-when-use-spritekit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!