Spritekit 停止声音 [英] Spritekit stopping sound

查看:31
本文介绍了Spritekit 停止声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Apple 的 SpriteKit 库开发游戏,但在开始播放后我无法停止声音.我尝试使用文档中列出的方法来阻止 Skaction 运行,但似乎没有任何东西可以阻止声音播放.这些是我试图阻止声音的尝试.

I'm currently developing a game using Apple's SpriteKit library but I'm unable to stop a sound after is has began playing. I have tried using the listed methods from the documentation in order to stop the Skaction from running but nothing seems to stop the sound from playing. These have been my attempts of trying to stop the sound.

//Playing sound action:

SKAction *music = [SKAction playSoundFileNamed:@"menu.wav" waitForCompletion:YES];
    [musicNode runAction:[SKAction repeatActionForever:[SKAction sequence:@[
                                                    music,
                                                    [SKAction waitForDuration:0],
                                                    ]]] withKey:@"MUSIC_PLAYING"];

//Later when i want to stop the sound:

[musicNode removeActionForKey:@"MUSIC_PLAYING"]; //Removing the action from the node
[musicNode runAction:[SKAction waitForDuration:0] withKey:@"MUSIC_PLAYING"]; //"Overriding" with another action with the same key
[musicNode removeAllActions]; //Removing all actions from the node
[musicNode removeFromParent]; //Removing the node from the parent

musicNode 只是一个运行动作的 SkpriteNode,尽管我直接从场景中尝试了相同的方法,即:场景播放声音,然后尝试删除它.或者,由于 waitForCompletion 设置为 YES,操作可能不会停止,但话又说回来,我尝试了 NO 和相同的结果.有没有办法使用 AVaudioSession 的 sharedInstante 来停止声音?我也尝试了以下方法,但也失败了:

The musicNode is simply an SkpriteNode that runs the action, though I tried the same methods directly from the scene i.e: The scene plays the sound and later attempts to remove it. Or maybe the action will not stop because of the waitForCompletion set to YES, but then again, I tried with NO and same results. Could there be someway of using AVaudioSession's sharedInstante to stop the sound? I also tried the following method but also failed:

 [[AVAudioSession sharedInstance]setActive:FALSE error:nil];

感谢您的帮助!

推荐答案

我在使用 SK 音频时也遇到过类似的问题.因此,我转而使用 AVFoundation.我不久前发现了这个;它可以完成我需要做的所有事情,而且听起来也可以解决您的问题!

Ive had similar issues using SK audio.. and as a result I have fallen back on using AVFoundation. I found this a while ago; it does everything I need it to do and sounds like it might fix your issue as well!

https://github.com/nicklockwood/SoundManager

这篇关于Spritekit 停止声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆