我想用SpriteKit
播放声音,问题是我使用的功能是:
[SKAction playSoundFileNamed:@"sound.wav" waitForCompletion:NO];
但是当我想暂停游戏时,声音仍在播放。
如何在skview.pause
设置为YES
时停止声音,并在skview.pause
设置为NO
时恢复声音?
答案 0 :(得分:9)
已经有一段时间了,但这是使用AVAudio可以做的事情的基础。我将在此示例中使用背景音乐,但它可以以任何方式完成。您可以将它放在SKSpriteNode的子类中,以获得可以自行暂停或自己暂停的自定义声音。
//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]
Here is the documentation for it, there are some cool things you can do with it.