本文介绍了使用SpriteKit时如何暂停声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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时如何暂停声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 03:03
查看更多