如何允许倒带但禁用html5-video中的快进

时间:2014-06-27 22:24:38

标签: javascript html5-video

我有以下代码(使用videojs)

this.on("timeupdate", function(event) {
            previousTime = this.currentTime();
        });

        this.on("seeking", function(){
            if (this.currentTime() > previousTime){
                this.currentTime = previousTime;
            }
        });

问题在于,一旦你尝试快进一次,它就不会让longers跟踪进度条上的timeupdate。我不能再在酒吧寻求 - 甚至不回放视频。我在这里缺少什么?

1 个答案:

答案 0 :(得分:5)

首先,这些不是标准的HTMLMediaElement方法和属性,所以我假设你正在使用某种框架,也许是Popcorn.js?我假设this指的是包装视频元素的对象。

问题在于您正在覆盖currentTime方法。如果您要直接引用视频元素,那么您可以通过设置currentTime属性来寻找您的方式,例如:videoElement.currentTime = previousTime。但是,由于您正在使用框架,this.currentTime应该是一个函数,直到您将其更改为数字。

要演示,请按如下方式修改代码:

this.on("seeking", function(){
    console.log(typeof this.currentTime); // 'function'
    if (this.currentTime() > previousTime){
        this.currentTime = previousTime;
        console.log(typeof this.currentTime); // 'number'
    }
});

下次运行“timeupdate”处理程序时,对this.currentTime()的调用将抛出错误,因为它不再是函数。

你可以这样解决它(假设你使用爆米花或类似的东西):

this.on("seeking", function(){
    if (this.currentTime() > previousTime){
        this.currentTime(previousTime);
    }
});

您还需要确保在搜索时不设置previousTime,因为“timeupdate”可能会在“搜索”之前触发。我不确定如何使用您的框架,所以这里是使用本机HTMLVideoElement API的工作示例的链接:

http://jsbin.com/kilemoto/1/edit