问题描述
我想使用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.
在skview.pause
设置为YES
时如何停止声音,在skview.pause
设置为NO
时如何恢复声音?
How to stop the sound when skview.pause
is set to YES
, and to resume the sound when skview.pause
is set to NO
?
推荐答案
已经有一段时间了,但这是您可以使用AVAudio进行操作的基础.在此示例中,我将使用背景音乐,但是可以通过任何方式完成.您可以将其放在SKSpriteNode的子类中,以具有可以自己或其他人暂停的自定义声音.
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];
然后开始使用[_player play];
使用[_player pause]
暂停.
和[_player stop]
这篇关于使用SpriteKit时如何暂停声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!