Jon Sud
Jon Sud

Reputation: 11641

How to add event listener to videojs when start to play a video?

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".

Upvotes: 12

Views: 23563

Answers (5)

scott
scott

Reputation: 1478

Simple using videoJS:

            var player = videojs('element_id', videojsOptions);

            player.on("play", function(){
                // whatever
            });

Upvotes: 1

Shlomi Levi
Shlomi Levi

Reputation: 3305

You can do this videojs way.

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

enter image description here

Upvotes: 21

JamesBot
JamesBot

Reputation: 161

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.

Upvotes: 2

Flash Thunder
Flash Thunder

Reputation: 12036

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

Upvotes: 0

Jenaro Calvi&#241;o
Jenaro Calvi&#241;o

Reputation: 193

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

Upvotes: 0

Related Questions