【问题标题】:event.target.setVolume(0); doesn't work by itself, why?event.target.setVolume(0);自己不行,为什么?
【发布时间】:2021-12-05 02:33:39
【问题描述】:
event.target.setVolume(0); // doesn't work by itself

其实在下一行

t.getVolume(); // spits 100, always

但是当它被放置在一个事件处理程序中时,它就可以了。为什么?

scroll.addEventListener('wheel', function (e) { changeVolume(e, this) });
    function changeVolume(event, el)
    {
        const dir = Math.sign(event.deltaY) * -1;
        const parsed = parseInt(el.value, 10);
        const value = Number(dir + parsed);
        t.setVolume(value); // TARGET
        // OH wait! Now - it does work. Why?
    }

【问题讨论】:

标签: javascript event-handling youtube-api dom-events


【解决方案1】:

好的。因此需要加载 iframe 以更改 -this-one-property-,其他一切正常 - set - get - : PlaybackRate, sekto, unMute, mute...一切!!

在目标 proto 中:存储具有所需体积的闭包并使用保护子句保护自己。

function onPlayerReady(event)
{
    const t = event.target;

    function printVol()
    {
        console.log(
            {
                getVol: t.getVolume(), //nope
                volume: t.__proto__.newVol
            }
        );
    }
    function readyToChangeVolumeOnce()
    {
        if (!t.__proto__.changedVolumeOnce)
        {
            t.__proto__.changedVolumeOnce = true;
            t.setVolume(t.__proto__.newVol);
            printVol(); // works as expected... right. Should I be doing this?
        }
    }

    // javascript is crazy
    t.__proto__.changedVolumeOnce = false;
    t.__proto__.readyToChangeVolumeOnce = readyToChangeVolumeOnce;
    t.__proto__.newVol = validVolume(); // 20 for example
}

一旦 iframe (API) 知道可以播放,然后调用 proto 内的函数。

function onStateChange(state)
{
    const t = state.target;
    if (state.data === YT.PlayerState.PLAYING)
    {
       t.__proto__.readyToChangeVolumeOnce(); //man...
    }
}

这是一种解决方法,我不认为这本身就是一个答案。我很高兴它可以工作。 我还注意到如果音量为0t.unmute() 分配的音量较低。 再次,大家,所有这一切都可能是我的主要误解。我不知道。

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2015-02-04
    • 2022-06-15
    • 2012-08-19
    • 2015-07-30
    相关资源
    最近更新 更多