【问题标题】:Spritekit stopping soundSpritekit 停止声音
【发布时间】:2014-04-21 06:45:58
【问题描述】:

我目前正在使用 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];

感谢您的帮助!

【问题讨论】:

    标签: sprite-kit audio avaudiosession skaction


    【解决方案1】:

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

    https://github.com/nicklockwood/SoundManager

    【讨论】:

    • 是的!你说得对,我环顾四周寻找类似的问题,确实切换到 AVFoundation 对我来说效果更好,而且在我的情况下,处理游戏音乐,因为我什至可以管理音量和暂停它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多