2016-03-16 62 views
1

我需要在視頻JS中創建一個自定義事件,在播放過程中的某個點觸發。在video-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!!!') 
}); 

我怎樣才能做到這一點?

回答

2

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); 
+0

謝謝,這真的很有幫助 –