ended event videojs not working

不想你离开。 提交于 2019-12-19 05:17:25

问题


I would like to trigger a simple event when my video is finished. I've placed this directly bellow my video tag.

     <script type="text/javascript">
        var myPlayer = videojs("session_video");
          videojs("session_video").ready(function(){
          this.addEvent("ended", function(){ 
            alert('Here I am');
          });
        });       
     </script>

However I get: TypeError: this.addEvent is not a function and I can't find why.

You can see it live here: 78.47.121.50 (stackoverflow won't allow to make that a link) enter code 01-01-01-2012 to bring up the video.

Any insight is much apricaited!

Kind regards, Jason.


回答1:


"addEvent" and "removeEvent" were replaced by "on" and "off"

try:

this.on("ended", function(){ 



回答2:


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');
                }
            });
    });



回答3:


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>



回答4:


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.



来源:https://stackoverflow.com/questions/16573974/ended-event-videojs-not-working

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