【问题标题】:visibility back to 'visible' : video auto-play only if was not on pause before可见性回到“可见”:只有在之前没有暂停时视频才会自动播放
【发布时间】:2015-10-11 00:22:29
【问题描述】:

我可以在可见性更改为隐藏时暂停视频,并在可见性恢复为可见时播放视频。像这样:

    var userManuallyPause = false;

    var video = document.getElementById('video');
    var documentTitle = document.title;

    var updateTitleForVideo = function(state){
        if (state === '') {
            document.title = documentTitle;
            return;
        };

        document.title = documentTitle + ' [' + state + ']';
    };

    video.onpause = function(){
        userManuallyPause = true;
        updateTitleForVideo('Paused');
    };

    video.onplay = function(){
        updateTitleForVideo('');
    };

    document.addEventListener('visibilitychange', function(){
        var state = document.visibilityState;

        if (!video.paused) {
            if (state === 'hidden') {
                video.pause();
                userManuallyPause = false;
                updateTitleForVideo('Paused');
            }
        }
        else if (state === 'visible' && !userManuallyPause) { video.play(); }
    });

但如果视频在可见性变为隐藏之前已经暂停,我不希望视频在我恢复可见时播放。

这可能吗?我不确定。

【问题讨论】:

    标签: javascript html5-video


    【解决方案1】:

    您可以将其存储在var userManuallyPause = false 等变量中,如果用户自己暂停它,您可以将该变量设置为true。当用户取消暂停时,您可以将其设置回 false。

    然后在上面的代码中,添加一个检查

    if (!userManuallyPause) { ... }
    

    它应该按预期运行。

    【讨论】:

    • 但是怎么做呢?如何区分用户手动暂停和事件生成的暂停?
    • 我猜用户手动暂停是由暂停按钮上的 onClick 事件生成的。在那个事件中,你设置了变量。
    • 我做不到。除非我看到一个完整的工作示例,否则我认为这是不可能的。
    • 你如何让用户暂停?
    • 用户可以使用此代码手动暂停吗?我没有看到来自用户点击或任何事件调用 video.onpause() 的事件。
    猜你喜欢
    • 2015-09-11
    • 2019-08-24
    • 2020-09-06
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2023-04-05
    • 2023-01-17
    • 2017-04-24
    相关资源
    最近更新 更多