10

How do I add event listener to videojs when the video is start to play? (this event should be called at the begging of the play)?

I searched on Player Events docs, but I can't find any event that tell me "now the video is start play".

Jon Sud
  • 10,211
  • 17
  • 76
  • 174
  • I'm not familiar with this player framework, but based on the demos it seems to me that it uses ` – bgazsi Dec 05 '19 at 13:50

4 Answers4

19

You can do this videojs way.

play.on('play', () => { });

enter image description here

Shlomi Levi
  • 3,114
  • 1
  • 23
  • 35
0

right there on the docs you have the timeupdate event, you could set a flag to true when it starts.

  • event time this function is invoked. I have to use a global var to know if it starts. I looking if videojs have this option – Jon Sud Dec 05 '19 at 13:49
0

That's only a "skin" for HTML5 player, you can access the original HTML5 element by .player() function, and then use those:

var vid = myplayer.player();
vid.onplay = function() {
    alert("The video has started to play");
};

player()

Return the component's player

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
0

I suggest checking the docs for the <video> element.

You will see many events are emitted. Most importantly,

play - Playback has begun.

We can add an event listener to the element listening for this event:

document.querySelector('.video').addEventListener('play',  evt => { 
   // code you want to happen when the video plays
});

Note: document.querySelector('.video') is just a filler, select the element however you want to

I suggest this over @FlashThunder's solution because you can add multiple listeners and for other reasons.

JamesBot
  • 131
  • 1
  • 2
  • 8