在video-js中如何创建自定义事件?

时间:2016-03-16 16:14:28

标签: javascript javascript-events html5-video video.js

我需要在视频JS中创建一个自定义事件,在播放期间的某个时刻触发。

例如:

player.on('timeupdate',function(){
    if(player.currentTime> 50){
        // trigger some custom event like 'played50Second'
    }
});

然后我想做:

player.on('played50Second',function(evt){
    console.log('Great Job, you played 50 second of the video!!!')
});

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

player.trigger('played50Second');

您可能希望确保只触发一次,而不是每次currentTime()大于50时。currentTime是video.js API中的一个函数,因此player.currentTime() > 50

    var player = videojs('my_player');
    var timeCheck = function() {
      if (player.currentTime() > 50) {
        player.off('timeupdate', timeCheck);
        player.trigger('played50Second');
      }
    };
    player.on('played50Second', function(){
      console.log('...');
    });
    player.on('timeupdate', timeCheck);