I would like to trigger a simple event when my video is finished. I\'ve placed this directly bellow my video tag.
"addEvent" and "removeEvent" were replaced by "on" and "off"
try:
this.on("ended", function(){
On Stackoverflow i find one part of solution the otherpart find it from Lyn Headley top answer.
<script type="text/javascript">
//rename subtitle button
videojs.addLanguage('de', {
"captions off": "Untertitel aus",
});
videojs(document.querySelector('video') /* or selector string */, {
techOrder: ['html5', 'flash'],
controls: true,
autoplay: false,
preload: 'false',
language: 'de',
flash: {
swf: 'video-js.swf'
}
}, function(){
// Player (this) is initialized and ready.
this.on('ended', function(){
alert('ended');
});
this.on('firstplay', function(){
alert('firstplay');
});
});
</script>
this will alert you on the end of video. place it under the code of your video and it should works.
The ended event will not fire if end user intentionally seek the video to end keeping video as paused. I was able to get it work as:
<script type="text/javascript">
var player = videojs('my_video', {}, function() {});
player.ready(function(){
var duration_time = Math.floor(this.duration());
this.on('timeupdate', function() {
var current_time = Math.floor(this.currentTime());
if ( current_time > 0 && ( current_time == duration_time ) ){
$('#continue_button').removeAttr("disabled");
}
});
});
</script>
2020 Updated answer:
let player = videojs('video-player-id');
player.on('ended', function() {
this.dispose();
});
I think it's good to call dispose More on why over here
For those who still have problems with ended events in some browsers you can resort to this fix:
player.ready(function() {
var myPlayer = this;
playerInstance.on("timeupdate", function(event) { //chrome fix
if (event.currentTarget.currentTime == event.currentTarget.duration) {
console.log('video ended');
}
});
});