کلاس برنامه را بسازید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
در application.js
، کلاس اصلی را برای برنامه HbbTV خود ایجاد کنید که با پخش HbbTV تعامل دارد. این کلاس با broadcastAppManager
و broadcastContainer
تعامل دارد. برای مثالی از یک کلاس مشابه، مدیریت شیء پخش a/v را ببینید.
این برنامه پایه HbbTV را تغییر دهید تا یک جریان IMA درخواست کند و به رویدادهای وقفه آگهی پاسخ دهد.
برنامه را راه اندازی کنید
کلاس برنامه را در application.js
راه اندازی کنید، broadcastAppManager
و broadcastContainer
به دنبال آموزش، Handling the broadcast a/v اجرا کنید . پس از آن، اشیاء VideoPlayer
و AdManager
جدید را راه اندازی کنید.
یک درخواست جریان IMA ارائه دهید
در روش HbbTVApp.onPlayStateChangeEvent()
، در پاسخ به تغییر برنامه به PRESENTING_PLAYSTATE
، درخواست پخش جریانی ایجاد کنید. این رویکرد برنامه شما را برای بارگیری مانیفست غلاف تبلیغاتی در پاسخ به یک رویداد AD_BREAK_EVENT_ANNOUNCE
آماده میکند.
اگر دستگاه شما رویداد PlayStateChange
محفظه پخش را به درستی منتشر نمی کند، از تابع setInterval()
برای بررسی تغییرات playstate استفاده کنید:
setInterval(function() {
if (!subscribedToStreamEvents &&
this.broadcastContainer.playState == PRESENTING_PLAYSTATE) {
subscribedToStreamEvents = true;
this.broadcastContainer.addStreamEventListener(
STREAM_EVENT_URL, 'eventItem', function(event) {
this.onStreamEvent(event);
}.bind(this));
debugView.log('Subscribing to stream events');
this.adManager.requestStream(NETWORK_CODE, CUSTOM_ASSET_KEY);
}
…
به رویدادهای جریان HbbTV گوش دهید
متد HbbTVApp.onStreamEvent()
را برای گوش دادن به رویدادهای ad break adBreakAnnounce
، adBreakStart
و adBreakEnd
ایجاد کنید:
رویدادهای جریان HbbTV را مدیریت کنید
برای مدیریت رویدادهای جریان HbbTV، مراحل زیر را انجام دهید:
برای بارگیری مانیفست غلاف تبلیغاتی در پاسخ به رویداد adBreakAnnounce
، متد HbbTVApp.onAdBreakAnnounce()
را ایجاد کنید:
برای جابجایی به پخش جریانی تبلیغات در طول وقفه های تبلیغاتی، متد HbbTVApp.onAdBreakStart()
را ایجاد کنید:
برای بازگشت به پخش محتوا، متد HbbTVApp.onAdBreakEnd()
را ایجاد کنید:
اکنون در حال درخواست و نمایش پادهای تبلیغات IMA SDK در برنامه HbbTV خود هستید. برای مقایسه برنامه خود با یک برنامه نمونه کامل، به نمونه IMA HbbTV در GitHub مراجعه کنید.
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-30 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-30 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThis guide details how to integrate the IMA SDK into an HbbTV application to enable dynamic ad insertion, building upon a pre-existing ads manager class.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves initializing the application, making an IMA stream request when the broadcast begins, and listening for HbbTV stream events signaling ad breaks.\u003c/p\u003e\n"],["\u003cp\u003eSpecific event handlers (\u003ccode\u003eonAdBreakAnnounce\u003c/code\u003e, \u003ccode\u003eonAdBreakStart\u003c/code\u003e, \u003ccode\u003eonAdBreakEnd\u003c/code\u003e) manage ad pod loading, ad playback, and the seamless transition back to broadcast content.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the provided code snippets for implementation guidance and to the IMA HbbTV sample on GitHub for a complete working example.\u003c/p\u003e\n"]]],[],null,["| **Note:** Before moving forward with this guide, you must complete the steps in [Create the ads manager class](/ad-manager/dynamic-ad-insertion/sdk/html5/hbbtv-ads-manager).\n\nIn `application.js`, create the main class for your HbbTV app that interacts\nwith the HbbTV broadcast. This class interacts with the `broadcastAppManager`\nand `broadcastContainer`. For an example of a similar class, see\n[Handling the broadcast a/v object](//developer.hbbtv.org/tutorials/handling-the-broadcast-av-object/).\n\nModify this base HbbTV app to request an IMA stream and respond to ad break\nevents.\n\nInitialize the application\n\nInitialize the application class in `application.js`, set up the\n`broadcastAppManager`, and `broadcastContainer` following the tutorial,\n[Handling the broadcast a/v object](//developer.hbbtv.org/tutorials/handling-the-broadcast-av-object/).\nAfterwards, initiate new `VideoPlayer` and `AdManager` objects. \n\n /** Main HbbTV Application. */\n var HbbTVApp = function() {\n this.broadcastAppManager = document.getElementById('broadcast-app-manager');\n this.broadcastContainer = document.getElementById('broadcast-video');\n\n this.playState = -1; // -1 as null play state.\n\n try {\n this.applicationManager =\n this.broadcastAppManager.getOwnerApplication(document);\n this.applicationManager.show();\n this.broadcastContainer.bindToCurrentChannel();\n this.subscribedToStreamEvents = false;\n this.broadcastContainer.addEventListener(\n 'PlayStateChange', this.onPlayStateChangeEvent.bind(this));\n\n debugView.log('HbbTVApp: App loaded');\n this.videoPlayer = new VideoPlayer();\n this.videoPlayer.setOnAdPodEnded(this.resumeBroadcast.bind(this));\n } catch (e) {\n debugView.log('HbbTVApp: No HbbTV device detected.');\n return;\n }\n\n this.adManager = new AdManager(this.videoPlayer);\n }; \n https://github.com/googleads/googleads-ima-html5-dai/blob/d215db4a053d12c842e899da470684142ff732f6/hbbtv/application.js#L39-L64\n\nMake an IMA stream request\n\nIn the `HbbTVApp.onPlayStateChangeEvent()` method, make a stream request in\nresponse to the app switching to the `PRESENTING_PLAYSTATE`. This approach\nprepares your app to load the ad pod manifest in response to an\n`AD_BREAK_EVENT_ANNOUNCE` event. \n\n if (!this.subscribedToStreamEvents &&\n this.broadcastContainer.playState == PRESENTING_PLAYSTATE) {\n this.subscribedToStreamEvents = true;\n this.broadcastContainer.addStreamEventListener(\n STREAM_EVENT_URL, 'eventItem', function(event) {\n this.onStreamEvent(event);\n }.bind(this));\n debugView.log('HbbTVApp: Subscribing to stream events.');\n this.adManager.requestStream(NETWORK_CODE, CUSTOM_ASSET_KEY);\n } \n https://github.com/googleads/googleads-ima-html5-dai/blob/d215db4a053d12c842e899da470684142ff732f6/hbbtv/application.js#L75-L84\n\nIf your device doesn't correctly emit the broadcast container `PlayStateChange`\nevent, use the `setInterval()` function to check for playstate changes: \n\n setInterval(function() {\n if (!subscribedToStreamEvents &&\n this.broadcastContainer.playState == PRESENTING_PLAYSTATE) {\n subscribedToStreamEvents = true;\n this.broadcastContainer.addStreamEventListener(\n STREAM_EVENT_URL, 'eventItem', function(event) {\n this.onStreamEvent(event);\n }.bind(this));\n debugView.log('Subscribing to stream events');\n this.adManager.requestStream(NETWORK_CODE, CUSTOM_ASSET_KEY);\n }\n ...\n\nListen to HbbTV stream events\n\nCreate the `HbbTVApp.onStreamEvent()` method to listen to the ad break events\n`adBreakAnnounce`, `adBreakStart`, and `adBreakEnd`: \n\n /**\n * Callback for HbbTV stream event.\n * @param {!Event} event Stream event payload.\n */\n HbbTVApp.prototype.onStreamEvent = function(event) {\n var eventData = JSON.parse(event.text);\n var eventType = eventData.type;\n if (eventType == AD_BREAK_EVENT_ANNOUNCE) {\n this.onAdBreakAnnounce(eventData);\n } else if (eventType == AD_BREAK_EVENT_START) {\n this.onAdBreakStart(eventData);\n } else if (eventType == AD_BREAK_EVENT_END) {\n this.onAdBreakEnd(eventData);\n }\n }; \n https://github.com/googleads/googleads-ima-html5-dai/blob/d215db4a053d12c842e899da470684142ff732f6/hbbtv/application.js#L94-L108\n\nHandle the HbbTV stream events\n\nTo handle the HbbTV stream events, complete the following steps:\n\n1. To load the ad pod manifest in response to the `adBreakAnnounce` event,\n create the `HbbTVApp.onAdBreakAnnounce()` method:\n\n /**\n * Callback function on ad break announce stream event.\n * @param {!Event} event HbbTV stream event payload.\n */\n HbbTVApp.prototype.onAdBreakAnnounce = function(event) {\n var eventType = event.type;\n var eventDuration = event.duration;\n var eventOffset = event.offset;\n debugView.log(\n 'HbbTV event: ' + eventType + ' duration: ' + eventDuration +\n 's offset: ' + eventOffset + 's');\n this.adManager.loadAdPodManifest(NETWORK_CODE, CUSTOM_ASSET_KEY, eventDuration);\n }; \n https://github.com/googleads/googleads-ima-html5-dai/blob/d215db4a053d12c842e899da470684142ff732f6/hbbtv/application.js#L138-L150\n\n2. To switch to ad stream playback during ad breaks, create the\n `HbbTVApp.onAdBreakStart()` method:\n\n /**\n * Callback function on ad break start stream event.\n * @param {!Event} event HbbTV stream event payload.\n */\n HbbTVApp.prototype.onAdBreakStart = function(event) {\n debugView.log('HbbTV event: ' + event.type);\n if (!this.videoPlayer.isPreloaded()) {\n debugView.log('HbbTVApp: Switch aborted. ' +\n 'The ad preloading buffer is insufficient.');\n return;\n }\n this.stopBroadcast();\n this.videoPlayer.play();\n }; \n https://github.com/googleads/googleads-ima-html5-dai/blob/d215db4a053d12c842e899da470684142ff732f6/hbbtv/application.js#L154-L167\n\n3. To return to the content broadcast, create the `HbbTVApp.onAdBreakEnd()`\n method:\n\n /**\n * Callback function on ad break end stream event.\n * @param {!Event} event HbbTV stream event payload.\n */\n HbbTVApp.prototype.onAdBreakEnd = function(event) {\n debugView.log('HbbTV event: ' + event.type);\n this.videoPlayer.stop();\n this.resumeBroadcast();\n }; \n https://github.com/googleads/googleads-ima-html5-dai/blob/d215db4a053d12c842e899da470684142ff732f6/hbbtv/application.js#L171-L179\n\nYou're now requesting and displaying IMA SDK ad pods in your HbbTV\napp. To compare your app with a completed sample app, see the\n[IMA HbbTV sample on GitHub](//github.com/googleads/googleads-ima-html5-dai/tree/main/hbbtv)."]]