html 5视频跟踪百分比

时间:2017-05-03 04:36:42

标签: javascript jquery ajax html5 logic

如何跟踪观看视频的状态

如果我在60%之后写这样的话,我可以发送ajax电话并将状态更新为观看60%

var i=0;
$("#video").bind("timeupdate", function(){
var currentTime = this.currentTime;
if(currentTime > 0.66*(this.duration)) {
    if(i<1) {
        /* Watched 66% ajax call will be here*/
    }
    i=i+1; //Reset for duplicates
}
});

同样。 如果我在100%之后这样写,我可以发送一个ajax调用并将状态更新为完全监视100%

$("#video").bind("ended", function() {
    if(j<1) {
        /* Watched 100% Finished */  
    }
    j=j+1; //Reset for duplicates
});

但是当有人将视频转发到90%并从那里开始播放时,那么100%的ajax调用也会触发,因此我应该使用什么逻辑将状态更新为在这种情况下不被观看。

2 个答案:

答案 0 :(得分:3)

如何操纵这样的功能

&#13;
&#13;
var watchPoints = [];

$("#video").bind("timeupdate", function(){
var currentTime = this.currentTime;
var watchPoint = Math.floor((currentTime/this.duration) * 100);
if(watchPoints.indexOf(watchPoint) == -1){
  watchPoints.push(Math.floor(watchPoint))
}
});

/* Assuming that this will be called regardless of whether the user watches till the end point */
$("#video").bind("ended", function() {
   /* use the watchPoints array to do the analysis(ordered array)
   Eg.1 [1,2,3,4,5....100] - length is 100
      2.[90,91..100]  - length is only 10 and the starting point is 90
      3.[1,2,3,4,5,50,51,52,61,62,63,64,65,66,67,68,69,70,98,99,100] - length is 21 and the index of 66 is 13 which clearly tells the user has skipped most of the parts
   */
});
&#13;
&#13;
&#13;

答案 1 :(得分:0)

通过研究和努力工作,我得到了这样的解决方案:

我的HTML是这样的:

<div id="videoData">
<video width="75%" style="max-width: 60em;" autoplay="" controls="" class="center-block" id="video">
<source type="video/mp4" src="http://amerytech.net/lc_production/uploads/course_videos/Time Speed &amp; Distance/Time Speed &amp; Distance.mp4" le="max-width: 60em;" sty="">
</source>
</video>
</div>  
<p id="display">asdddddd</p>

我的javascript是这样的:

$(document).ajaxStop(function() {
        var video = document.querySelector('video'); 
        if (video) {
            video.addEventListener("loadedmetadata", function() { 
                totalTime = this.duration;
            });
            $("#video").bind("timeupdate", function() {
                var currentTime = this.currentTime;
                var totalPlayed = 0;
                var played = video.played;
                for (var i = 0; i < played.length; i++) {
                    totalPlayed += played.end(i) - played.start(i);
                    console.log(totalPlayed);
                    $("#display").html(totalPlayed);
                }
            }); 
            } else { 
            alert("not coming"); 
        } 
    });