在 ads_manager.js 中,为 IMA SDK StreamManager 定义一个封装容器类,该类用于发出视频流请求、获取广告插播时段清单、监听 IMA 视频流事件,并将 emsg 事件传递给 IMA SDK。
在 ads_manager.js 中,IMA HbbTV 示例应用设置了以下方法:
requestStream()
onStreamEvent()
onEmsgEvent()
loadAdPodManifest()
初始化广告管理器
初始化广告管理器类,并为 IMA 流事件设置监听器。在此调用中,使用 VideoPlayer.setEmsgEventHandler() 方法设置 emsg 事件处理脚本。
/** * Wraps IMA SDK ad stream manager. * @param {!VideoPlayer} videoPlayer Reference an instance of the wrapper from * video_player.js. */varAdManager=function(videoPlayer){this.streamData=null;this.videoPlayer=videoPlayer;// Ad UI is not supported for HBBTV, so no 'adUiElement' is passed in the// StreamManager constructor.this.streamManager=newgoogle.ima.dai.api.StreamManager(this.videoPlayer.videoElement);this.streamManager.addEventListener([google.ima.dai.api.StreamEvent.Type.STREAM_INITIALIZED,google.ima.dai.api.StreamEvent.Type.ERROR,google.ima.dai.api.StreamEvent.Type.CLICK,google.ima.dai.api.StreamEvent.Type.STARTED,google.ima.dai.api.StreamEvent.Type.FIRST_QUARTILE,google.ima.dai.api.StreamEvent.Type.MIDPOINT,google.ima.dai.api.StreamEvent.Type.THIRD_QUARTILE,google.ima.dai.api.StreamEvent.Type.COMPLETE,google.ima.dai.api.StreamEvent.Type.AD_BREAK_STARTED,google.ima.dai.api.StreamEvent.Type.AD_BREAK_ENDED,google.ima.dai.api.StreamEvent.Type.AD_PROGRESS,google.ima.dai.api.StreamEvent.Type.PAUSED,google.ima.dai.api.StreamEvent.Type.RESUMED],this.onStreamEvent.bind(this),false);this.videoPlayer.setEmsgEventHandler(this.onEmsgEvent,this);};
创建 AdManager.requestStream() 方法,以使用您的 Google Ad Manager 广告资源网代码和视频流的自定义素材资源键来创建 PodStreamRequest 对象。使用 IMA 示例 DASH pod 提供以下串流参数的串流来测试 HbbTV 应用:
广告资源网代码:'21775744923'
自定义资源键:'hbbtv-dash'
/** * Makes a pod stream request. * @param {string} networkCode The network code. * @param {string} customAssetKey The custom asset key. */AdManager.prototype.requestStream=function(networkCode,customAssetKey){varstreamRequest=newgoogle.ima.dai.api.PodStreamRequest();streamRequest.networkCode=networkCode;streamRequest.customAssetKey=customAssetKey;streamRequest.format='dash';debugView.log('AdsManager: make PodStreamRequest');this.streamManager.requestStream(streamRequest);};
创建 AdManager.onStreamEvent() 方法来处理应用对 IMA 流事件 STREAM_INITIALIZED、AD_BREAK_STARTED 和 AD_BREAK_ENDED 的响应。
/** * Handles IMA playback events. * @param {!Event} event The event object. */AdManager.prototype.onStreamEvent=function(event){switch(event.type){// Once the stream response data is received, generate pod manifest url// for the video stream.casegoogle.ima.dai.api.StreamEvent.Type.STREAM_INITIALIZED:debugView.log('IMA SDK: stream initialized');this.streamData=event.getStreamData();break;casegoogle.ima.dai.api.StreamEvent.Type.ERROR:break;// Hide video controls while ad is playing.casegoogle.ima.dai.api.StreamEvent.Type.AD_BREAK_STARTED:debugView.log('IMA SDK: ad break started');this.adPlaying=true;this.adBreakStarted=true;break;// Show video controls when ad ends.casegoogle.ima.dai.api.StreamEvent.Type.AD_BREAK_ENDED:debugView.log('IMA SDK: ad break ended');this.adPlaying=false;this.adBreakStarted=false;break;// Update ad countdown timers.casegoogle.ima.dai.api.StreamEvent.Type.AD_PROGRESS:break;default:debugView.log('IMA SDK: '+event.type);break;}};
/** * Callback on Emsg event. * Instructs IMA SDK to fire back VAST events accordingly. * @param {!Event} event The event object. */AdManager.prototype.onEmsgEvent=function(event){vardata=event.event.messageData;varpts=event.event.calculatedPresentationTime;if((datainstanceofUint8Array) && data.byteLength > 0){this.streamManager.processMetadata('ID3',data,pts);}};
创建 AdManager.loadAdPodManifest() 方法,以使用视频播放器预加载广告连播清单。使用方法:DASH Pod 清单中的结构构建清单网址。
/** * Creates DAI pod url and instructs video player to load manifest. * @param {string} networkCode The network code. * @param {string} customAssetKey The custom asset key. * @param {number} podDuration The duration of the ad pod. */AdManager.prototype.loadAdPodManifest=function(networkCode,customAssetKey,podDuration){if(!this.streamData){debugView.log('IMA SDK: No DAI pod session registered.');return;}varMANIFEST_BASE_URL='https://dai.google.com/linear/pods/v1/dash/network/';// Method: DASH pod manifest reference docs:// https://developers.google.com/ad-manager/dynamic-ad-insertion/api/pod-serving/reference/live#method_dash_pod_manifestvarmanifestUrl=MANIFEST_BASE_URL+networkCode+'/custom_asset/'+customAssetKey+'/stream/'+this.streamData.streamId+'/pod/'+this.getPodId()+'/manifest.mpd?pd='+podDuration;this.videoPlayer.preload(manifestUrl);};
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-10。"],[[["This guide outlines how to integrate the IMA SDK into an HbbTV app to manage ad streams, including setting up an `AdManager` class to handle stream requests and events."],["For HbbTV, it is crucial to omit the `adUiElement` in the `StreamManager` constructor to avoid serving incompatible ads, as the IMA SDK does not support ad UI on this platform."],["The `AdManager` class defines methods to request ad streams, handle IMA events (like `STREAM_INITIALIZED`, `AD_BREAK_STARTED`, and `AD_BREAK_ENDED`), and process ad stream metadata using emsg events."],["Ad pod manifests are preloaded by constructing a URL using Google Ad Manager parameters (network code, custom asset key) and stream information (`streamId`, `podId`), ensuring the `podId` remains consistent for viewers of the same ad break."],["Before proceeding with these steps, complete the setup outlined in the \"Set up the player class\" guide to lay the foundation for the HbbTV player."]]],["The `ads_manager.js` file defines an `AdManager` class to manage IMA SDK stream interactions. This includes `requestStream()` for sending stream requests using a network code and custom asset key, `onStreamEvent()` to handle IMA stream events like `STREAM_INITIALIZED`, `AD_BREAK_STARTED`, and `AD_BREAK_ENDED`. `onEmsgEvent()` passes emsg event data to the IMA SDK, and `loadAdPodManifest()` preloads the ad pod manifest using a constructed URL. The `podId` should be unique for each ad break.\n"]]