javascript - 防止视频加载时重置 currentTime?

标签 javascript html video

我希望能够将视频重新加载到 HTML5 视频中,而无需在加载时重置 currentTime。我目前的做法如下:

<button onclick="getCurTime()" type="button">Get current time position</button>
<button onclick="setCurTime()" type="button">Set time position to 5 seconds</button><br> 
<div style="width:800px; height:445px;">
<video id="myVideo" width="100%" height="100%" controls="controls">
 <source src="http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4" type="video/mp4">
</video>
<script>
var vid = document.getElementById("myVideo");

function setCurTime() { 
    vid.currentTime=100;
} 
$(document).ready(function () 
{ 
    $('#myVideo').videocontrols( 
    { 
        preview: 
        { 
            sprites: ['big_bunny_108p_preview.jpg'], 
            step: 10, 
            width: 200 
        }, 
        theme: 
        { 
            progressbar: 'blue', 
            range: 'pink', 
            volume: 'pink' 
        } 
    }); 
    vid.play();
}); 
setInterval(function(){ 
    if(vid.currentTime > vid.duration-1)
    {
    myVideo.src = "http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4";
myVideo.load();
myVideo.play();
vid.currentTime = vid.duration-60*5
}
 }, 1);
</script> 
</div>

我该如何去做呢?有没有办法只更新视频播放器中的数据而无需重新加载视频?我希望能够做到这一点,这样如果有人对视频进行修改,它只会更新视频播放器中的数据,这样用户就不必再次重新加载整个视频。

最佳答案

根据上面评论线程中的讨论,我仍然不能 100% 确定为什么要重新加载同一视频,因此我可能会丢失一些上下文,但以下代码将允许您更改视频源但保留当前时间。它确实假定 jQuery 作为事件处理程序(尽管您可以轻松地在同一事件上使用常规 javascript 事件处理程序来执行相同的操作)

<video id="v" width="320" height="240" controls="controls" mute>
<source src="Video.mp4" />
</video>
<button onclick="reload()">Reload</button>

<script>
function reload() {
    vid=document.getElementById("v")
    // record the current time for the video that is playing
    curTime = vid.currentTime
    // set the source for the replacement video...
    vid.src = "http://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4";
    // ... and load it
    vid.load();
    // add event handler for "canplay" to set the time, and then start the video
    $("#v").on("canplay",function() {
        vid.currentTime = curTime
        vid.play();
        // remove the event to stop it triggering multiple times
        $("#v").off("canplay")
    })
}
</script>

关于javascript - 防止视频加载时重置 currentTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27727495/

相关文章:

javascript - 从 Canvas 中删除饼图并替换为另一个饼图

javascript - Angular JS/Ui 路由器 - 在 StateParams 中发布图像 URL

javascript - 在网页(不是本地主机)上实时显示图片(位于计算机上)

c# - 是否可以通过编程方式确定视频是否正在播放?

cocoa - 我可以让 QTKit Capture 支持 IP 摄像头吗?

javascript - 打印时将多个html表格分成不同的页面

javascript - 网络音频API : How to use FFT to convert from time domain and use iFFT to convert the data back

php创建DOM元素并使用ajax发送

html - 如何为选择框的下拉设置高度

video - ffmpeg 分布式转码