6

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

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

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

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

4

1 回答 1

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]

这是它的文档,你可以用它做一些很酷的事情。

于 2013-12-16T22:10:57.090 回答