2017-04-13 68 views
0

我使用視頻html5元素獲取持續時間屬性以查找最後一次視頻在用戶關閉瀏覽器或用戶暫停時停止播放的位置 將其發送到數據庫稍後它將從停止的地方恢復。使用jquery(Html5視頻元素)恢復視頻

目前我用Google搜索和檢查堆棧溢出是這樣

<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 
     }) 
    }); 
+0

'window.onbeforeunload = video.onpause = E => postToYourServer(video.currentTime)' – Kaiido

回答

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/