【问题标题】:Resume video using Jquery(Html5 video element)使用 Jquery(Html5 视频元素)恢复视频
【发布时间】:2017-04-15 15:51:01
【问题描述】:

我正在使用视频 html5 元素来获取持续时间属性以查找最后一次 当用户关闭浏览器或用户暂停时视频停止播放的位置,将其发送到数据库,以便稍后从停止的位置继续播放。

目前我用谷歌搜索并检查堆栈溢出是这样的

    <script type="text/javascript">

    $("#video").on("durationchange", function() {
         alert("Current duration is: " + this.duration);
     }); 

  </script>

 <video id="video" poster="image.jpg" controls>     
      <source src="video_path.mp4" type="video/mp4" />
  </video>

我还想寻找视频到用户登录后停止的持续时间 到我的应用程序。我试过了,但不是正确的做法

       $(window).load(function() {
       .ajax({
            type: "GET",
            url: "/api/videoResume/",
            data: param = "",
            contentType: "application/json; charset=utf-8",
            dataType: "json"              
        })
        .done(function(result){
           //update the video element duration 
           //seeking video to the duration
        })
   });

【问题讨论】:

  • window.onbeforeunload = video.onpause = e =&gt; postToYourServer(video.currentTime)

标签: javascript jquery html html5-video


【解决方案1】:

另一种方法是添加一个事件侦听器,这样当视频暂停时,您可以捕获当前时间,然后您可以将该信息发送到您的数据库,而不是像下面的示例中那样发送警报等等。代码如下所示:

Javascript:

var video1 = document.getElementById('video1');

function videoPausePlayHandler(e) {
    if (e.type == 'pause') {
   alert(video1.currentTime);
    }
}

video1.addEventListener('pause', videoPausePlayHandler, false);

HTML:

<video id="video1" poster="image_path.jpg" controls>
<source src="video_path.mp4" type="video/mp4" />
</video>

工作样本:https://jsfiddle.net/l33tstealth/qr72bvxb/1/

【讨论】:

    猜你喜欢
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2013-06-04
    • 1970-01-01
    • 2015-10-24
    • 2015-11-26
    • 1970-01-01
    相关资源
    最近更新 更多