iOS Safari在刷卡时无法解锁Web AudioContext

时间:2016-06-23 08:30:19

标签: javascript mobile-safari web-audio

iOS不允许在没有用户输入的情况下使用Web Audio来触发它。为了解决这个问题,我们有一个touchend监听器来执行此操作:

       initAudio: function () {

            // create a blank buffer
            var buffer = this._atx.createBuffer(1, 1, 22050); // this._atx is the AudioContext
            var node = this._atx.createBufferSource();
            node.buffer = buffer;

            node.start(0); // or noteOn if the browser doesn't support this, removed that check/code for brevity
        }

在大多数情况下,这样做很好。我们在游戏上有一个覆盖,拦截第一次点击,并调用上述功能。在函数结束时,this._atx.state"running"(在"suspended"调用之前为node.start(0)

但是,如果通过屏幕上的快速滑动触发回调,而不是点击,则会通过此代码,并且在函数结束时,状态仍为"suspended"。它两次执行完全相同的代码,唯一的区别是用户输入的性质。

这是添加侦听器的代码:

        this._boundSoundTapped = this._onSoundTapped.bind(this);
        this._confirmPopup.addEventListener("touchend", this._boundSoundTapped);
        this._confirmPopup.addEventListener("click", this._boundSoundTapped);

onSoundTapped函数:

    _onSoundTapped: function(e){
        e.stopPropagation();
        e.preventDefault();
        if(this._soundsPressed === false) {
            this._confirmPopup.removeEventListener("touchend", this._boundSoundTapped);
            this._confirmPopup.removeEventListener("click", this._boundSoundTapped);
            this._soundsPressed = true;
            this._player.initAudio();
        }
    },

我真的不明白为什么滑动而不是点击会产生不同的效果,它们都会触发touchend,并且无论如何都会执行相同的代码。

0 个答案:

没有答案