objective-c – 使用SpriteKit时如何暂停声音

我想用SpriteKit播放声音,问题是我使用的功能:
[SKAction playSoundFileNamed:@"sound.wav" waitForCompletion:NO];

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

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

解决方法

已经过了一段时间,但这是使用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.

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...