Closed. This question needs to be more focused。它当前不接受答案。












想改善这个问题吗?更新问题,使其仅关注editing this post一个问题。

去年关闭。



Improve this question




不同之处在于,如果有人在播放视频时离开该人,则该人可以返回视频页面,并且该站点将从用户离开时开始自动播放。
目前,我正在使用YouTube Player API加载视频。

我的 friend 该如何解决这个问题?
我不确定,但是我正在尝试用这个关键词进行研究。
但是仍然没有完成。
“如何跟踪用户在YouTube Player API中观看视频花了多少时间”。

最佳答案

使用YT API并在play上启动计时器,在pausestop上停止计时器。不太准确,因为用户可能会离开播放器,但仍未观看视频,但可能会这样做。

很久以前就使用YT API,但很简单,您可以像使用javascript中的任何其他事件一样挂接到事件,并且只需启动/停止计时器(或用于测量时间的任何计时器)即可。最初将total_viewed_time设置为零,然后在play上计算start_time,并在stop / pause / finish上按查看时间更新total_viewed_time,即end_time-start_time。例如看下面:

var total_viewed_time = 0, start_time = 0, end_time = 0;

// assume we have a yt video which we we hook for events (see YT API for exactly how to hook)
ytvideo.on('play', function( ){
    start_time = new Date().getTime(); // milliseconds
    end_time = start_time;
});
ytvideo.on('pause stop finish', function( ){
   end_time = new Date().getTime(); // milliseconds
   total_viewed_time += end_time-start_time; // milliseconds
});

// total_viewed_time measures how much time (in milliseconds) the user had the player on (maybe watching?)
// you can then convert this time to seconds or minutes or whatever suits you
// to get the time in seconds do: total_viewed_time/1000
// to get the time in minutes do: total_viewed_time/(60*1000)
// and so on..

另外,如果应用程序需要检查用户是否已切换浏览器选项卡或窗口,则该应用程序可以侦听blurunfocus事件并停止计时器,然后在浏览器选项卡/窗口中重新启动它。focus
  • Is there a way to detect if a browser window is not currently active?
  • Check if window has focus
  • Detect If Browser Tab Has Focus
  • 10-08 09:18