bibliophilsagar
bibliophilsagar

Reputation: 1753

to add event listener for getting video duration in run time in videojs

I want to monitor the video duration as the video plays using any event listener. I am using videojs for video playback.

I came across events like play, myplayer.isPaused but they fire only once. myplayer is the video element for videojs. Now upon further research i got an event named on. So here goes the code :

myPlayer.on("play", myfun);

  function myfun() {
    console.log("myPlayer.currentTime()");
    console.log(myPlayer.currentTime());
    myfun();
  }

Here is what i expected that it will give me currentTime as you can see i am calling myfun recursively. But this function is getting called a million times but only gives me the starting time which is 0.

I even tried to put myPlayer.currentTime() in setInterval, but this method is not trustworthy and ofcourse not a good idea.

I used native javascript eventlisteners but they are not likely to be useful.

So any help to attach event listener for the videoplayback sothat i would get the play duration?

Upvotes: 4

Views: 7469

Answers (1)

Svetlin Mladenov
Svetlin Mladenov

Reputation: 4427

The play event is fired only when playback starts. If you want to monitor progress into the video then the correct event is timeupdate. Here is an example:

myPlayer.on("timeupdate", myfun);

function myfun() {
    console.log("myPlayer.currentTime()", myPlayer.currentTime());
}

Upvotes: 7

Related Questions