加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:45:45 所属栏目:百科 来源:网络整理
导读:我想用SpriteKit播放声音,问题是我使用的功能: [SKAction playSoundFileNamed:@"sound.wav" waitForCompletion:NO]; 但是当我想暂停游戏时,声音仍在播放. 如何在skview.pause设置为YES时停止声音,并在skview.pause设置为NO时恢复声音? 解决方法 已经过了一
我想用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读