mediaelement / mediaelement-plugins

Plugins for the main mediaelement project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chromecast ended event

cedrictailly opened this issue · comments

It seems the "ended" event is not always fired at the end of a media played through a chromecast device, this event is detected by checking if currentTime is upper or equal to duration, but this way is unstable because sometimes it's missed.

To solve this, search :

t.controller.addEventListener(cast.framework.RemotePlayerEventType.CURRENT_TIME_CHANGED, function () {
  var event = mejs.Utils.createEvent('timeupdate', t.media);
  t.media.dispatchEvent(event);

  if (!t.isLive && t.getCurrentTime() >= t.getDuration()) {
    t.endedMedia = true;
    setTimeout(function () {
      var event = mejs.Utils.createEvent('ended', t.media);
      t.media.dispatchEvent(event);
    }, 50);
  }
});

...and replace by :

t.controller.addEventListener(cast.framework.RemotePlayerEventType.CURRENT_TIME_CHANGED, function () {
  var event = mejs.Utils.createEvent('timeupdate', t.media);
  t.media.dispatchEvent(event);
});

t.controller.addEventListener(cast.framework.RemotePlayerEventType.PLAYER_STATE_CHANGED, function (e) {
  if ( e.value == 'BUFFERING' )
    t.endedMedia = false;
  else if ( !t.endedMedia && e.value === null ){
    t.endedMedia = true;
    var event = mejs.Utils.createEvent('ended', t.media);
    t.media.dispatchEvent(event);
  }
});

...and you have to initialize t.endedMedia to true just before :

var t = this;
t.player = player;
t.controller = controller;
t.media = media;
t.endedMedia = true;
t.enableTracks = options.castEnableTracks;
t.isLive = options.castIsLive;