ตั้งค่าคลาสโปรแกรมเล่น

ให้เสร็จสมบูรณ์ก่อนจึงจะดําเนินการต่อตามคู่มือนี้ได้

ใน video_player.js ให้กำหนดคลาส Wrapper ของวิดีโอเพลเยอร์เพื่อเริ่มต้นและควบคุมเพลเยอร์ dash.js

ตั้งค่าโปรแกรมเล่นบรอดแบนด์

กำหนดตําแหน่งในแอปที่จะวางโปรแกรมเล่นบรอดแบนด์โดยสร้างแท็กวิดีโอและแท็ก Wrapper ดังนี้

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

สร้างวิดีโอเพลเยอร์

เริ่มต้นคลาสวิดีโอเพลเยอร์ด้วยตัวแปรสำหรับองค์ประกอบ HTML, โปรแกรมเล่น dash.js และ Callback ที่เมธอดอื่นๆ ของคลาสสามารถใช้ได้

/**
 * 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() method หลังจากนั้น ให้สร้างเมธอด create 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();
};

โหลดไฟล์ Manifest ของสตรีมโฆษณาล่วงหน้า

ใช้ VideoPlayer.preload() และ VideoPlayer.isPreloaded() เพื่อให้แน่ใจว่าโฆษณาจะโหลดได้เพียงพอระหว่างสตรีมเนื้อหาและก่อนที่ช่วงพักโฆษณาจะเริ่ม

1. โหลดสตรีมโฆษณาล่วงหน้า

สร้างเมธอด VideoPlayer.preload() เพื่อโหลดไฟล์ Manifest ของสตรีมโฆษณาล่วงหน้าและสร้างบัฟเฟอร์โฆษณาก่อนช่วงพักโฆษณา คุณต้องอัปเดตการตั้งค่าสตรีมมิงของเพลเยอร์ '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;
};

แนบ Listener ของโปรแกรมเล่น

หากต้องการเพิ่ม Listeners เหตุการณ์สำหรับเหตุการณ์ของโปรแกรมเล่น dash.js ให้สร้างVideoPlayer.attachPlayerListener()เมธอด PLAYBACK_PLAYING, PLAYBACK_ENDED, LOG และ ERROR นอกจากนี้ วิธีการนี้ยังจัดการเหตุการณ์สําหรับ 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);
    }
  };
};

ตั้งค่า Callback ของเหตุการณ์ของผู้เล่น

หากต้องการจัดการการเล่นพ็อดโฆษณาตามเหตุการณ์ของโปรแกรมเล่น ให้สร้างเมธอด 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

ตั้งค่าฟังก์ชัน Callback ที่ทำงานเมื่อพ็อดโฆษณาสิ้นสุดลงโดยสร้างเมธอด 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;
};

จัดการเหตุการณ์ข้อมูลเมตาของสตรีม

ตั้งค่าฟังก์ชันการเรียกกลับที่ทํางานตามเหตุการณ์ emsg โดยสร้างเมธอด VideoPlayer.setEmsgEventHandler() สําหรับคู่มือนี้ ให้ใส่พารามิเตอร์ scope เมื่อคุณเรียกใช้ setEmsgEventHandler() นอก video_player.js

/**
 * 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 ในการส่งคําขอสตรีม รับไฟล์ Manifest ของพ็อดโฆษณา ฟังเหตุการณ์สตรีม IMA และส่งเหตุการณ์ emsg ไปยัง IMA SDK