我目前正在使用 Apple 的 SpriteKit 库开发一款游戏,但在游戏开始播放后我无法停止声音。我尝试使用文档中列出的方法来阻止 Skaction 运行,但似乎没有什么可以阻止声音播放。这些都是我试图阻止声音的尝试。
//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来停止声音?我也尝试过以下方法,但还是失败:
[[AVAudioSession sharedInstance]setActive:FALSE error:nil];
谢谢你的帮助!
我在使用 SK audio 时也遇到过类似的问题。因此我又转而使用 AVFoundation。我不久前发现了这个;它可以完成我需要它做的所有事情,听起来它也可以解决您的问题!
https://github.com/nicklockwood/SoundManager
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)