如何在javascript中播放视频时获取搜索时间

时间:2017-09-19 08:59:12

标签: javascript html5-video

我试图在视频播放时获得寻道时间。我使用播放事件只触发视频开始。

HTML:

<video controls id="videoFile">
   <source src="Why Linux over Windows 3D Animation.mp4" id="video_here"> 
</video>

Javasript:

var secondvideo = document.getElementById('videoFile');
secondvideo.addEventListener('play', function(e) { 
    // The video is playing
    console.log("current time= "+ document.getElementById('videoFile').currentTime);

});

2 个答案:

答案 0 :(得分:2)

来自MDN

  

HTMLMediaElement.currentTime属性提供当前播放   时间以秒为单位。设置此值会将媒体搜索到新的时间。

根据这些信息,您应该执行以下操作:

var secondvideo = document.getElementById('videoFile');
secondvideo.addEventListener('play', function(e) { 
    // The video is playing
    console.log("Playing video");
    console.log(secondvideo.currentTime);
});
secondvideo.addEventListener('pause', function(e) { 
    // The video is paused
    console.log("Paused video");
    console.log(secondvideo.currentTime);
});
secondvideo.addEventListener('seeking', function(e) { 
    // The user seeked a new timestamp in playback
    console.log("Seeking in video");
    console.log(secondvideo.currentTime);
});

答案 1 :(得分:1)

secondvideo.addEventListener("timeupdate",
   function (ev) {
      console.log(ev.target.currentTime);
   });

https://www.w3.org/TR/html5/embedded-content-0.html#handler-mediacontroller-ontimeupdate

您可以在此处找到HTML5视频事件和API:https://www.w3.org/2010/05/video/mediaevents.html