หากต้องการพูดคุยและแสดงความคิดเห็นเกี่ยวกับผลิตภัณฑ์ของเรา โปรดเข้าร่วมช่อง Discord อย่างเป็นทางการของ Ad Manager ในเซิร์ฟเวอร์ชุมชนการโฆษณาและการวัดผลของ Google
ใน ads_manager.js ให้กำหนดคลาส Wrapper สำหรับ StreamManager ของ IMA SDK ที่สร้างคำขอสตรีม รับไฟล์ Manifest ของพ็อดโฆษณา ฟังเหตุการณ์สตรีม IMA และส่งเหตุการณ์ emsg ไปยัง IMA SDK
ใน ads_manager.js แอปตัวอย่าง IMA HbbTV จะตั้งค่าเมธอดต่อไปนี้
requestStream()
onStreamEvent()
onEmsgEvent()
loadAdPodManifest()
เริ่มต้นใช้งาน Ad Manager
เริ่มต้นคลาสตัวจัดการโฆษณาและตั้งค่า Listener สำหรับเหตุการณ์สตรีม IMA ในการเรียกนี้ ให้ตั้งค่าเครื่องจัดการเหตุการณ์ emsg ด้วยเมธอด
VideoPlayer.setEmsgEventHandler()
/** * 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()เมธอดเพื่อสร้างPodStreamRequestออบเจ็กต์โดยใช้รหัสเครือข่าย Ad Manager ของ Google และคีย์ชิ้นงานที่กำหนดเองของสตรีม ทดสอบแอป HbbTV โดยใช้สตรีมการแสดงโฆษณาพ็อด DASH ของ IMA ตัวอย่างที่มีพารามิเตอร์สตรีมต่อไปนี้
รหัสเครือข่าย: '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();varsecretKey=getStreamSecretKeys();streamRequest.networkCode=networkCode;streamRequest.customAssetKey=customAssetKey;streamRequest.format='dash';if(secretKey.STREAM_CREATE_SECRET){varexpirationTime=Math.trunc(newDate().getTime()/1000)+3600;varparams={custom_asset_key:customAssetKey,exp:expirationTime,network_code:networkCode};streamRequest.authToken=this.generateAuthToken(params,secretKey.STREAM_CREATE_SECRET);debugView.log('AdsManager: make Auth-PodStreamRequest');}else{debugView.log('AdsManager: make PodStreamRequest without auth');}this.streamManager.requestStream(streamRequest);};
/** * 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;}};
หากต้องการส่งข้อมูลเหตุการณ์ emsg ไปยัง IMA ให้สร้างเมธอด AdManager.onEmsgEvent()
โดยใช้เมธอด StreamManager.processMetadata() คลาสวิดีโอเพลเยอร์เรียกใช้เมธอดนี้ด้วยเมธอด VideoPlayer.setEmsgEventHandler()
/** * 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);}};
/** * 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('AdsManager: No stream data available.');return;}varadBreakId=this.getAdBreakId();varexpirationTime=Math.trunc(newDate().getTime()/1000)+3600;varparams={ad_break_id:adBreakId,custom_asset_key:customAssetKey,exp:expirationTime,network_code:networkCode,pd:podDuration};varsecretKey=getStreamSecretKeys();vartoken=this.generateAuthToken(params,secretKey.MANIFEST_SECRET);varencodedToken=encodeURIComponent(token);varmanifestUrl='https://dai.google.com/linear/pods/v1/dash/network/'+networkCode+'/custom_asset/'+customAssetKey+'/stream/'+this.streamData.streamId+'/ad_break_id/'+adBreakId+'/manifest.mpd?pd='+podDuration+'&auth-token='+encodedToken;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"]],["อัปเดตล่าสุด 2026-04-17 UTC"],[],["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"]]