Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ontimeupdate with YouTube API

HTML5 video supports the ontimeupdate event; is there an equivalent in the YouTube API that either isn't listed in the docs, or just that I'm unaware of? I've tried a setInterval method to check every n milliseconds, but then I have to check to see if the video is playing, etc. Has anyone dealt with this issue?

like image 456
Ian Hunter Avatar asked Mar 28 '12 19:03

Ian Hunter


1 Answers

According to the documentation there is no such an event (I think you are also aware of this). Other answers also state this and as far as I have seen over code examples, there is no direct equivalent. Even in some of the code examples (check the getting started code), they rely on setTimeout in order to do something 6 seconds after the video has started.

Some people have find work arounds, for example in this answer, they create a timer so they can actually fire every X milliseconds a time check:

function onPlayerReady() {
  function updateTime() {
    var oldTime = videotime;
    if(player && player.getCurrentTime) {
      videotime = player.getCurrentTime();
    }
    if(videotime !== oldTime) {
      onProgress(videotime);
    }
  }
  timeupdater = setInterval(updateTime, 100);
}

In this way you could actually simulate the event timeupdate, you just need also to clear the timeout on state change (if it is stopped you do not want to continuously call the function).

Some people have create a YouTube Player API wrapper like HTML5 video API. In that one you are actually accessing the HTML5 video API in the embedded Youtube Video.

I am not sure of the roadmap of the Youtube API, but probably they will move to a more generic and standard api that is compatible with the current HTML5 Video one, but until then I hope my answer can help you

like image 192
SirPeople Avatar answered Sep 21 '22 13:09

SirPeople