प्लेयर क्लास सेट अप करना

video_player.js में, dash.js प्लेयर को शुरू करने और कंट्रोल करने के लिए, वीडियो प्लेयर रैपर क्लास तय करें.

ब्रॉडबैंड प्लेयर सेट अप करना

वीडियो और रैपर टैग बनाकर, यह तय करें कि आपको अपने ऐप्लिकेशन में ब्रॉडबैंड प्लेयर कहां रखना है:

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

वीडियो प्लेयर बनाना

एचटीएमएल एलिमेंट, 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() तरीका बनाएं. इसके बाद, विज्ञापन पॉड के खत्म होने के बाद, क्लीन-अप को मैनेज करने के लिए, 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_PLAYING, PLAYBACK_ENDED, LOG, और ERROR. यह तरीका, स्कीम आईडी यूआरआई के इवेंट भी हैंडल करता है. साथ ही, इन लिसनर को हटाने के लिए क्लीन-अप फ़ंक्शन सेट करता है.

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 इवेंट के आधार पर काम करता है. इस गाइड के लिए, scope पैरामीटर शामिल करें, क्योंकि आपने video_player.js से बाहर setEmsgEventHandler() को ट्रिगर किया है.

/**
 * 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 स्ट्रीम इवेंट सुनने, और IMA SDK को ईएमएसजी इवेंट पास करने के लिए, विज्ञापन मैनेजर क्लास बनाएं.