首页 > 解决方案 > 在 videojs 中将 currentTime 设置为任何值都会将时间设置回 0

问题描述

我正在用 videojs 做一些相当简单的事情。我只是想通过单击外部构建的 UI 将时间设置为时间线上的任意点(不是默认的搜索内容,而是一组类似于进度条的 div。)

所有文档和每篇文章似乎都表明在 object.currentTime(time) 中设置 currentTime 是有效的。但就我而言,它没有。不在控制台中,不在任何地方。

这是我的代码:

function clearSetTimeBug(videoObj,timeToSetTo){     
    console.log('timeToSetTo:'+timeToSetTo);
    console.log("-1."+videoObj.id()+" readyState(): "+videoObj.readyState());//returns 4 (loaded)
    console.log("0."+videoObj.id()+" bufferedEnd(): "+videoObj.bufferedEnd());//returns about 2 seconds ahead of where the playhead is)
    if(videoObj.bufferedEnd()>timeToSetTo){
        console.log("1."+videoObj.id()+" currentTime(): "+videoObj.currentTime()); // returns a valid videojs object id and then shows the currentTime to be where the video's current timecode as a float.
        videoObj.currentTime(timeToSetTo);//this should totally work!
        console.log("2."+videoObj.id()+" currentTime(): "+videoObj.currentTime());// shows that the current time is 0!
    }else{ 
        //I added this to just go to as far as it's buffered, but this doesn't work either
        console.log("3."+videoObj.id()+" currentTime(): "+videoObj.currentTime());
        console.log("4. newTime: "+videoObj.bufferedEnd());
        videoObj.currentTime(videoObj.bufferedEnd());
        pageTime=videoObj.bufferedEnd();
        console.log("5."+videoObj.id()+": "+videoObj.currentTime());// still shows that the current time is 0!
        console.log("6. pageTime:"+pageTime);
    }
}

播放器是这样旋转的:

function setUpPlayers(playerId,playlist,local_file,index){
    let myAspectRatio=heightRatio+":1"
    
    let videoTag="";
    videoTag +="<video id='playerInstance"+index+"' class='video-js vjs-fluid' width='100%' height='100%'>\n";
    videoTag +="\t<source src='"+playlist+"' type='video/mp4'>\n";
    videoTag +="</video>\n"
    $('#player'+index).html(videoTag);
    window["vjPlayer"+index]=videojs("playerInstance"+index, {"autoplay": false, "preload": "auto" });
}

Play() 和 pause() 甚至 currentTime() 都可以获取当前时间。但试图设置它只会迫使玩家回到 0。

我正在使用 Chrome 版本 86.0.4240.80 和 Firefox 开发者版 71.0b1(64 位)的 Mac 工作。这些都是本地文件,我从 http://localhost:8000/index.php 运行它。

标签: video.js

解决方案


在 video.js github 频道中,我找到了这个问题的答案。我没有在我的服务器上检查它,所以在本地运行页面会破坏点击和设置 currentTime 的能力。很奇怪,其实。我仍然不确定为什么会这样。你可以在这里看到评论:https ://github.com/videojs/video.js/issues/6900

对我来说这不是最聪明的举动,但希望这可能会在未来对其他人有所帮助。


推荐阅读