gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:41:55 25 4
gpt4 key购买 nike

我想用SpriteKit来播放声音,问题是我用的函数:

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

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

如何在 skview.pause 设置为 YES 时停止声音,以及在 skview.pause 设置为时恢复声音

最佳答案

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

关于objective-c - 如何在使用 SpriteKit 时暂停声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com