播放时获取youtube视频的当前时间

时间:2018-11-03 20:47:17

标签: javascript html youtube

当我这样播放Youtube视频时:

https://www.youtube.com/tv?#/watch/video/control?v=d1-VK12FZhs&resume&t=0m4s

我在视频底部看到播放时间和持续时间。

我可以在页面中找到当前的播放时间:

  

0:11

一段时间后,此信息将隐藏,直到我再次将鼠标指针移到视频上。

当显示信息时,我可以每秒更新一次ytp-time-current值。 隐藏信息时,不会更新此值。 隐藏信息后如何获取当前时间?

我在chrome扩展程序中使用以下代码:

document.addEventListener("spfdone", process); 
document.addEventListener("DOMContentLoaded", process); 

function process(){ 
stop(); 
start(); 
} 
window.onbeforeunload = function () { 
stop(); 
}; 

function start(){ 

x2 = setInterval(function() { 

// here is the place to use the solution i'm looking for

},1000); 
} 

function stop(){ 
}

谢谢。

2 个答案:

答案 0 :(得分:2)

根据YouTube iframe API,您可以使用:getCurrentTime()

在chrome控制台中编写以下代码就足够了:

document.querySelector('.video-stream').getCurrentTime()

答案 1 :(得分:0)

这是您想要显示的完整解决方案:

var player = document.getElementById('movie_player');
var time = player.getCurrentTime();
var sec_num = parseInt(time, 10);
var hours   = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10)
   hours = '0' + hours;
if (minutes < 10)
   minutes = '0' + minutes;
if (seconds < 10)
   seconds = '0' + seconds;
alert(hours + ':' + minutes + ':' + seconds);
相关问题