当HTML5视频搜索/播放时触发功能



我正在尝试使用每 2 分钟过期一次的链接流式传输视频。

基本上,我使用这个函数来替换 URL,效果很好:

function test(){
        var videoFile = 'new.mp4';
        var $video = $('#m video');
        var curtime = $video[0].currentTime;
        videoSrc = $('source', $video).attr('src', videoFile);
        $video[0].load();
        $video[0].currentTime = (curtime);
        $video[0].play();
    }

我的问题是,每次视频开始播放/有人寻求它之后,我如何触发此功能?如果我触发 ok();函数使用播放事件,然后启动循环,因为函数本身会导致播放事件。

有没有人对如何以好的方式解决这个问题有任何想法?

解决方案是在视频实际开始playing后注册play事件。这样,它将在暂停或搜索后做出反应。

如果您需要在其他条件下禁用该事件,则只需禁用play事件(如在playing函数开始时所做的那样)...

function test(){
    var videoFile = 'trailer.mp4';
    var $video = $('#video');
    var curtime = $video[0].currentTime;
    videoSrc = $('source', $video).attr('src', videoFile);
    $video[0].load();
    $video[0].currentTime = (curtime);
    $video[0].play();
    $video.on('playing', function () {
        $video.off('play') // remove existing Play event if there is one
        console.log("Play event bound")
        $video.on('play', function () {
            console.log("Video play. Current time of videoplay: " + $video[0].currentTime );
        });
    });
}

最新更新