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

﹥>﹥吖頭↗ 提交于 2021-01-28 04:36:56

问题


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


回答1:


You can do this videojs way.

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




回答2:


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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/59196447/how-to-add-event-listener-to-videojs-when-start-to-play-a-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!