Home Ask Login Register

Developers Planet

Your answer is one click away!

sagar patro February 2016

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() {

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?


Svetlin Mladenov February 2016

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());

Post Status

Asked in February 2016
Viewed 2,703 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life