Getting current time of embedded iframe youtube player?

五迷三道 提交于 2019-12-01 20:44:19
Jon B

This thread describes how you can get the current playing time of a video using the YouTube API Getting Current YouTube Video Time

Then maybe use setInterval() to run every second to check the value of getCurrentTime() from the API and if value = 6 set the src to the new video.

this should be solved with setInterval(), not setTimeout().

using the latest youtube iFrame API:

var player;
var checkInt; // save this as a var in this scope so we can clear it later
function onYouTubePlayerAPIReady() {
   player = new YT.Player('player');
   startInterval()
}

function startInterval() {
   //checks every 100ms to see if the video has reached 6s
   checkInt = setInterval(function() {
      if (player.getCurrentTime() > 6) {
         changeSrc();
         clearInterval(checkInt);
      };
   }, 100)
}

function changeSrc() {
   player.loadVideoById(xxxxx);
}

call below function when your video is starting playing

function StartVideo(){
setTimeout("ChangeLinkUrl()", 6000);
}

function ChangeLinkUrl(){
var elem = document.getElementById('YourFrameID');
 elem.src = "newSorceURL";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!