Spritekit stopping sound

只愿长相守 提交于 2019-12-01 17:54:00

问题


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

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];

Thanks for any help!


回答1:


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



来源:https://stackoverflow.com/questions/22431049/spritekit-stopping-sound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!