Skip to content

Commit

Permalink
fix: wait for source before preloading the entry (#45)
Browse files Browse the repository at this point in the history
Following kaltura/playkit-js#197 playing an ad can be before sourceselected event, so we have to consider this use case when ima preloads the source
  • Loading branch information
yairans authored Feb 25, 2018
1 parent a8ddea5 commit bd286c9
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/ima-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export default class ImaMiddleware extends BaseMiddleware {
*/
play(next: Function): void {
if (!this._isPlayerLoaded) {
this._context.player.load();
this._isPlayerLoaded = true;
this._context.logger.debug("Player loaded");
this._loadPlayer();
}
this._context.loadPromise.then(() => {
let sm = this._context.getStateMachine();
Expand Down Expand Up @@ -107,4 +105,22 @@ export default class ImaMiddleware extends BaseMiddleware {
}
}
}

/**
* Load the player.
* @returns {void}
* @private
*/
_loadPlayer(): void {
const loadPlayer = () => {
this._context.logger.debug("Load player by ima middleware");
this._context.player.load();
this._isPlayerLoaded = true;
};
if (this._context.player.engineType) { // player has source to play
loadPlayer();
} else {
this._context.player.addEventListener(this._context.player.Event.SOURCE_SELECTED, () => loadPlayer());
}
}
}

0 comments on commit bd286c9

Please sign in to comment.