如何在使用SpriteKit时暂停声音

时间:2013-11-03 15:58:43

标签: objective-c audio sprite-kit

我想用SpriteKit播放声音,问题是我使用的功能是:

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

但是当我想暂停游戏时,声音仍在播放。

如何在skview.pause设置为YES时停止声音,并在skview.pause设置为NO时恢复声音?

1 个答案:

答案 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.