プレーヤー クラスを設定する

video_player.js で、dash.js プレーヤーを開始して制御する動画プレーヤー ラッパークラスを定義します。

ブロードバンド プレーヤーを設定する

動画タグとラッパータグを作成して、ブロードバンド プレーヤーをアプリのどこに配置するかを定義します。

<div id="broadband-wrapper">
    <video id="broadband-video"></video>
</div>

動画プレーヤーを作成する

HTML 要素、dash.js プレーヤー、他のクラスメソッドで使用できるコールバックの変数を使用して、動画プレーヤー クラスを初期化します。

/**
 * Video player wrapper class to control ad creative playback with dash.js in
 * broadband.
 */
var VideoPlayer = function() {
  this.videoElement = document.querySelector('video');
  this.broadbandWrapper = document.getElementById('broadband-wrapper');
  this.player = dashjs.MediaPlayer().create();
  this.onAdPodEndedCallback = null;

  // Function passed in VideoPlayer.prototype.setEmsgEventHandler.
  this.onCustomEventHandler = null;
  //  Scope (this) passed in VideoPlayer.prototype.setEmsgEventHandler.
  this.customEventHandlerScope = null;

  // Function to remove all of player event listeners.
  this.cleanUpPlayerListener = null;
  debugView.log('Player: Creating dash.js player');
};

再生コントロール関数を定義する

広告プレーヤーを表示して動画ビューをアタッチするには、VideoPlayer.play() メソッドを作成します。次に、広告 Pod の終了後のクリーンアップを処理する VideoPlayer.stop() メソッドを作成します。

/** Starts playback of ad stream. */
VideoPlayer.prototype.play = function() {
  debugView.log('Player: Start playback');
  this.show();
  this.player.attachView(this.videoElement);
};

/** Stops ad stream playback and deconstructs player. */
VideoPlayer.prototype.stop = function() {
  debugView.log('Player: Request to stop player');
  if (this.cleanUpPlayerListener) {
    this.cleanUpPlayerListener();
  }
  this.player.reset();
  this.player.attachView(null);
  this.player.attachSource(null);
  this.player = null;
  this.hide();
};

広告ストリーム マニフェストをプリロードする

コンテンツ ストリーム中と広告ブレークの開始前に広告が十分に読み込まれるようにするには、VideoPlayer.preload()VideoPlayer.isPreloaded() を使用します。

1. 広告ストリームをプリロードする

VideoPlayer.preload() メソッドを作成して、広告ストリーム マニフェストをプリロードし、広告ブレークの前に広告バッファを構築します。プレーヤーのストリーミング設定 'cacheInitSegments'true に更新する必要があります。設定を更新すると、init セグメントのキャッシュ保存が有効になり、広告に切り替える際の遅延を回避できます。

/**
 * Starts ad stream prefetching into Media Source Extensions (MSE) buffer.
 * @param {string} url manifest url for ad stream playback.
 */
VideoPlayer.prototype.preload = function(url) {
  if (!this.player) {
    this.player = dashjs.MediaPlayer().create();
  }
  debugView.log('Player: init with ' + url);
  this.player.initialize(/* HTMLVideoElement */ null, url, /* autoplay */ true);

  this.player.updateSettings({
    'debug': {
      'logLevel': dashjs.Debug.LOG_LEVEL_WARNING,
      'dispatchEvent': true,  // flip to false to hide all debug events.
    },
    'streaming': {
      'cacheInitSegments': true,
    }
  });
  this.player.preload();
  this.attachPlayerListener();
  debugView.log('Player: Pre-loading into MSE buffer');
};

2. プリロードされた広告バッファを確認する

VideoPlayer.isPreloaded() メソッドを作成して、アプリで設定されたバッファしきい値と比較して十分な広告バッファがプリロードされているかどうかを確認します。

// Ads will only play with 10 or more seconds of ad loaded.
var MIN_BUFFER_THRESHOLD = 10;
/**
 * Checks if the ad is preloaded and ready to play.
 * @return {boolean} whether the ad buffer level is sufficient.
 */
VideoPlayer.prototype.isPreloaded = function() {
  var currentBufferLevel = this.player.getDashMetrics()
      .getCurrentBufferLevel('video', true);
  return currentBufferLevel >= MIN_BUFFER_THRESHOLD;
};

プレーヤー リスナーをアタッチする

dash.js プレーヤー イベントのイベント リスナーを追加するには、VideoPlayer.attachPlayerListener() メソッド(PLAYBACK_PLAYINGPLAYBACK_ENDEDLOGERROR)を作成します。このメソッドは、これらのリスナーを削除するクリーンアップ関数を設定するだけでなく、スキーム ID URI のイベントも処理します。

var SCHEME_ID_URI = 'https://developer.apple.com/streaming/emsg-id3';
/** Attaches event listener for various dash.js events.*/
VideoPlayer.prototype.attachPlayerListener = function() {
  var playingHandler = function() {
    this.onAdPodPlaying();
  }.bind(this);
  this.player.on(dashjs.MediaPlayer.events['PLAYBACK_PLAYING'], playingHandler);
  var endedHandler = function() {
    this.onAdPodEnded();
  }.bind(this);
  this.player.on(dashjs.MediaPlayer.events['PLAYBACK_ENDED'], endedHandler);
  var logHandler = function(e) {
    this.onLog(e);
  }.bind(this);
  this.player.on(dashjs.MediaPlayer.events['LOG'], logHandler);
  var errorHandler = function(e) {
    this.onAdPodError(e);
  }.bind(this);
  this.player.on(dashjs.MediaPlayer.events['ERROR'], errorHandler);

  var customEventHandler = null;
  if (this.onCustomEventHandler) {
    customEventHandler =
        this.onCustomEventHandler.bind(this.customEventHandlerScope);
    this.player.on(SCHEME_ID_URI, customEventHandler);
  }

  this.cleanUpPlayerListener = function() {
    this.player.off(
        dashjs.MediaPlayer.events['PLAYBACK_PLAYING'], playingHandler);
    this.player.off(dashjs.MediaPlayer.events['PLAYBACK_ENDED'], endedHandler);
    this.player.off(dashjs.MediaPlayer.events['LOG'], logHandler);
    this.player.off(dashjs.MediaPlayer.events['ERROR'], errorHandler);
    if (customEventHandler) {
      this.player.off(SCHEME_ID_URI, customEventHandler);
    }
  };
};

プレーヤー イベント コールバックを設定する

プレーヤー イベントに基づいて連続配信広告の再生を管理するには、VideoPlayer.onAdPodPlaying()VideoPlayer.onAdPodEnded()VideoPlayer.onAdPodError() メソッドを作成します。

/**
 * Called when ad stream playback buffered and is playing.
 */
VideoPlayer.prototype.onAdPodPlaying = function() {
  debugView.log('Player: Ad Playback started');
};

/**
 * Called when ad stream playback has been completed.
 * Will call the restart of broadcast stream.
 */
VideoPlayer.prototype.onAdPodEnded = function() {
  debugView.log('Player: Ad Playback ended');
  this.stop();
  if (this.onAdPodEndedCallback) {
    this.onAdPodEndedCallback();
  }
};

/**
 * @param {!Event} event The error event to handle.
 */
VideoPlayer.prototype.onAdPodError = function(event) {
  debugView.log('Player: Ad Playback error from dash.js player.');
  this.stop();
  if (this.onAdPodEndedCallback) {
    this.onAdPodEndedCallback();
  }
};

onAdPodEnded イベントのセッターを作成する

VideoPlayer.setOnAdPodEnded() メソッドを作成して、広告連続配信が終了したときに実行されるコールバック関数を設定します。アプリクラスは、このメソッドを使用して、ミッドロール挿入点の後にコンテンツのブロードキャストを再開します。

/**
 * Sets a callback function for when an ad pod has ended.
 * @param {!Function} callback Callback function.
 */
VideoPlayer.prototype.setOnAdPodEnded = function(callback) {
  this.onAdPodEndedCallback = callback;
};

ストリーム メタデータ イベントを処理する

VideoPlayer.setEmsgEventHandler() メソッドを作成して、emsg イベントに基づいて実行されるコールバック関数を設定します。このガイドでは、video_player.js の外部で setEmsgEventHandler() を呼び出すため、scope パラメータを含めます。

/**
 * Sets emsg event handler.
 * @param {!Function} customEventHandler Event handler.
 * @param {!Object} scope JS scope in which event handler function is called.
 */
VideoPlayer.prototype.setEmsgEventHandler = function(
    customEventHandler, scope) {
  this.onCustomEventHandler = customEventHandler;
  this.customEventHandlerScope = scope;
};

ミッドロール挿入点の動画プレーヤーの表示と非表示を切り替える

ミッドロール挿入点中に動画プレーヤーを表示し、ミッドロール挿入点の終了後にプレーヤーを非表示にするには、VideoPlayer.show() メソッドと VideoPlayer.hide() メソッドを作成します。

/** Shows the video player. */
VideoPlayer.prototype.show = function() {
  debugView.log('Player: show');
  this.broadbandWrapper.style.display = 'block';
};

/** Hides the video player. */
VideoPlayer.prototype.hide = function() {
  debugView.log('Player: hide');
  this.broadbandWrapper.style.display = 'none';
};

次に、IMA SDK を使用してストリーム リクエストを行い、広告連続配信マニフェストを取得し、IMA ストリーム イベントをリッスンし、emsg イベントを IMA SDK に渡す広告マネージャー クラスを作成します。