【问题标题】:Stop Sound playing on Node in SceneKit停止在 SceneKit 中的节点上播放声音
【发布时间】:2018-07-09 14:48:57
【问题描述】:

我有一个手势识别器的以下代码,它会在被点击时播放喇叭,并在释放时停止:

@objc func didPressHorn(_ sender: UILongPressGestureRecognizer) {

    let tapLocation = sender.location(in: sceneView)

    if sender.state == .began {

        if (wasNodeTapped(node: hornNode!, tapLocation: tapLocation)) {
            hornNode!.runAction(SCNAction.playAudio(hornSound, waitForCompletion: false), forKey: "Horn")
            hornPlaying = true
        }

    } else if sender.state == .ended {
        if hornPlaying {
            // tried all the possible solutions, but the sound won't stop
            hornNode?.removeAction(forKey: "Horn")
            hornNode?.removeAllActions()
            hornNode?.isPaused = true
            hornSound.volume = 0
            hornPlaying = false
            print("Horn off") //gets printed!
        }
    }
}

如您所见,我正在尝试所有可能的解决方案(我知道),并且打印语句也被打印出来。但是,声音不会关闭。你有什么想法,为什么?

【问题讨论】:

    标签: ios objective-c swift scenekit arkit


    【解决方案1】:

    我不知道这是否是最好的想法/做法,但您可以尝试:self. hornNode.removeAllAudioPlayers()

    希望对您有所帮助!

    【讨论】:

    • 感谢您的回复!实际上,这很奇怪:首先它看起来像是在工作,因为喇叭继续响,释放后它按预期停止。但是,如果它再次点击它,什么也没有发生,所以喇叭保持关闭......你知道为什么吗?
    • 啊,那是因为现在以某种方式 volume = 0 和 isPaused = true 语句正在工作。当我删除时,喇叭按预期工作:) 但是,我仍然想知道,为什么其他语句不起作用......你有什么想法,为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2021-09-03
    相关资源
    最近更新 更多