Detect if HTML5 Video element is playing

后端 未结 16 2545
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 00:43

I\'ve looked through a couple of questions to find out if an HTML5 element is playing, but can\'t find the answer. I\'ve looked at the W3 documentation and it has an event n

16条回答
  •  误落风尘
    2020-11-29 01:16

    Here is what we are using at http://www.develop.com/webcasts to keep people from accidentally leaving the page while a video is playing or paused.

    $(document).ready(function() {
    
        var video = $("video#webcast_video");
        if (video.length <= 0) {
            return;
        }
    
        window.onbeforeunload = function () {
            var htmlVideo = video[0];
            if (htmlVideo.currentTime < 0.01 || htmlVideo.ended) {
                return null;
            }
    
            return "Leaving this page will stop your video.";
        };
    }
    

提交回复
热议问题