IMA HTML5 SDK از لیست پخش تبلیغات کاملاً خودکار پشتیبانی می کند. این ویژگی هنگام قاچاق تبلیغات شما، شکست های تبلیغاتی را همانطور که در Google Ad Manager مشخص شده است در محتوا قرار می دهد. همچنین کد پخشکننده ویدیو را که برای پشتیبانی از وقفههای تبلیغاتی ضروری است، از جمله قبل از پخش، میان پخش و پس از پخش، سادهتر میکند.
هنگام ایجاد AdsManager ، یک شی contentPlayback با استفاده از تماس getAdsManager ارسال می شود. این شی باید دارای ویژگی currentTime باشد که موقعیت پخش فعلی ویدیو را برمی گرداند. اگر از عنصر video HTML5 برای نمایش محتوای خود استفاده می کنید، می توانید آن عنصر را به SDK منتقل کنید. این شیء برای ردیابی پیشرفت بازپخش محتوا استفاده می شود، بنابراین وقفه های تبلیغاتی به طور خودکار در زمان های مشخص شده در Ad Manager درج می شوند. همچنین باید به SDK اطلاع دهید که میخواهید وضعیت محتوا را از طرف شما مدیریت کند.
varadsRenderingSettings=newgoogle.ima.AdsRenderingSettings();adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete=true;adsManager=adsManagerLoadedEvent.getAdsManager(videoContent,adsRenderingSettings);// See API reference for contentPlayback.
برای اطمینان از پخش شدن پستها، باید به SDK اطلاع دهید که محتوای شما تمام شد. این کمی مشکل است، زیرا در برخی موارد SDK از پخشکننده ویدیوی شما برای پخش تبلیغات استفاده میکند، بنابراین باید مطمئن شوید که فقط زمانی که محتوای شما تمام میشود به SDK اطلاع میدهید و نه زمانی که یک تبلیغ تمام میشود. با استفاده از کد زیر می توانید این کار را انجام دهید:
اگر پخشکننده ویدیوی شما از کشیدن به جستجو پشتیبانی میکند و ویژگی زمان فعلی پخشکننده ویدیو در حالی که کاربر در حال کشیدن است بهروزرسانی میشود، SDK نمیتواند بین محتوایی که به طور عادی پیشرفت میکند و کاربری که در جستجوی محتوا است، تفاوت قائل شود. برای getAdsManager باید از یک شی contentPlayback سفارشی به عنوان پارامتر خود استفاده کنید. برای مثالی از این مورد، به مشکل جستجو مراجعه کنید.
توجه: وقتی پخش محتوا تمام شد یا کاربر پخش را متوقف کرد، حتماً با AdsLoader.contentComplete تماس بگیرید تا به SDK پیام دهید که محتوا تمام شده است. SDK سپس وقفه تبلیغاتی پس از پخش را پخش می کند، در صورتی که برنامه ریزی شده باشد. رویداد ALL_ADS_COMPLETED زمانی مطرح میشود که همه وقفههای تبلیغاتی پخش شده باشند. علاوه بر این، توجه داشته باشید که ردیابی محتوا با فراخوانی init() شروع می شود و همیشه باید قبل از پخش محتوا، init() را فراخوانی کنید.
پخش خودکار وقفه های تبلیغاتی را غیرفعال کنید
در برخی شرایط ممکن است بخواهید از پخش وقفه های تبلیغاتی SDK جلوگیری کنید تا زمانی که برای آنها آماده باشید. در این سناریو، میتوانید بازپخش خودکار وقفههای تبلیغاتی را غیرفعال کنید تا به SDK اطلاع دهید که چه زمانی برای پخش وقفه تبلیغاتی آماده هستید. با این پیکربندی، زمانی که SDK یک وقفه تبلیغاتی را بارگیری کرد، یک رویداد AD_BREAK_READY را اجرا میکند. هنگامی که پخش کننده شما برای شروع استراحت آگهی آماده است، می توانید با ()adsManager.start تماس بگیرید.
functionrequestAds(){}...adsLoader.getSettings().setAutoPlayAdBreaks(false);...}functiononAdsManagerLoaded(){...// For non-auto ad breaks, listen for ad break readyadsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY,adBreakReadyHandler);...}functionadBreakReadyHandler(){// Once we're ready to play ads. To skip this ad break, simply return// from this handler without calling adsManager.start().adsManager.start();}
آن را امتحان کنید
برای اجرای کار کد زیر را ببینید.
مشکل با جستجو
اگر از قوانین تبلیغات استفاده می کنید، ممکن است در جستجوی کلیک و کشیدن با مشکل مواجه شوید. به طور خاص، پس از کلیک و کشیدن یک کاربر برای جستجوی ویدیو از چندین پادهای میانروول، ممکن است قبل از ازسرگیری محتوا، دو یا چند مورد از آن پادها را ببیند که پشت سر هم پخش میشوند. این به دلیل به روز رسانی زمان پخش ویدیو در حالی است که کاربر به دنبال آن است. اگر زمانی که کاربر به دنبال یک تبلیغ است، SDK برای زمان فعلی نظرسنجی شود، ممکن است فکر کند که تبلیغ باید پخش شود. وقتی محتوا از سر گرفته میشود، آن آگهی و سپس جدیدترین آگهی از زمان جستجو را پخش میکند. برای نمایش تصویری این مشکل، نمودار زیر را ببینید:
زمانی که کاربر شروع به جستجو می کند، زمان فعلی را ذخیره کنید و زمانی را که SDK درخواست می کند، گزارش دهید تا زمانی که کاربر پخش عادی را از سر بگیرد. برای نمایش تصویری این راه حل، نمودار زیر را ببینید:
با این راه حل، شما به درستی از 0:10 اواسط رول صرف نظر می کنید و فقط 0:20 mid-roll را بازی می کنید. این کار با استفاده از یک ردیاب هد پخش سفارشی در قطعه کد زیر انجام می شود. این کد شامل تغییراتی (به صورت پررنگ) از ads.js در نمونه پیشرفته HTML5 موجود در صفحه دانلود ما است.
این روش باید روی هر پلتفرمی به جز سافاری موبایلی کار کند. در سافاری موبایل، ویژگی جستجوی تگ ویدیو به درستی اجرا نمی شود (همیشه false را برمی گرداند). برای دور زدن آن، باید خودتان بررسی کنید تا ببینید آیا کاربر در جستجوی ویدیو است یا خیر. کد نمونه برای این روش به شرح زیر است. مجدداً، خطوط پررنگ اصلاحاتی در کد موجود هستند.
varAds=function(application,videoPlayer){...this.currentTime=0;setInterval(this.updateCurrentTime,1000);this.seeking=false;this.seekCheckInterval=1000;// You may need to adjust this value, depending on your platformthis.seekThreshold=100;this.previousTime=0;setInterval(Application.bind(this,this.checkForSeeking),this.seekCheckInterval);};Ads.prototype.updateCurrentTime=function(){if(!this.seeking){this.currentTime=this.videoPlayer_.contentPlayer.currentTime;}};Ads.prototype.checkForSeeking=function(){varcurrentTime=this.videoPlayer_.contentPlayer.currentTime;// How much time has passed since you last ran this method, in millisecondsvardiff=(currentTime-this.previousTime)*1000;// If that difference is greater than the time since you last ran this method,// plus the threshold, the user was seekingif(Math.abs(diff)>this.interval+this.threshold){this.seeking=true;}else{this.seeking=false;}// Grab the current video time again to make up for time spent in this methodpreviousTime=this.videoPlayer_.contentPlayer.currentTime;};Ads.prototype.onAdsManagerLoaded_=function(adsManagerLoadedEvent){this.application_.log('Ads loaded.');this.adsManager_=adsManagerLoadedEvent.getAdsManager(this);this.processAdsManager_(this.adsManager_);};
با این تغییرات، SDK اکنون از ویژگی currentTime شی Ads شما برای تعیین زمان پخش وقفه های تبلیغاتی استفاده می کند، نه از ویژگی currentTime پخش کننده ویدیوی محتوا.
تاریخ آخرین بهروزرسانی 2025-09-10 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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"]],["تاریخ آخرین بهروزرسانی 2025-09-10 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThe IMA HTML5 SDK enables automated ad playlists, simplifying ad integration and supporting pre-rolls, mid-rolls, and post-rolls by leveraging Google Ad Manager.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure proper ad playback, developers must signal content completion using \u003ccode\u003eAdsLoader.contentComplete()\u003c/code\u003e and manage content state synchronization with the SDK.\u003c/p\u003e\n"],["\u003cp\u003eCustom content playback objects can be used to address seeking issues and provide finer control over ad break timing and behavior.\u003c/p\u003e\n"],["\u003cp\u003eThe SDK provides events like \u003ccode\u003eCONTENT_PAUSE_REQUESTED\u003c/code\u003e, \u003ccode\u003eCONTENT_RESUME_REQUESTED\u003c/code\u003e, and \u003ccode\u003eAD_BREAK_READY\u003c/code\u003e for developers to manage content playback and ad interactions.\u003c/p\u003e\n"],["\u003cp\u003eSpecific code adjustments are necessary to handle seeking accurately on mobile Safari due to platform limitations.\u003c/p\u003e\n"]]],[],null,["Select platform: [HTML5](/interactive-media-ads/docs/sdks/html5/client-side/ad-rules \"View this page for the HTML5 platform docs.\") [Android](/interactive-media-ads/docs/sdks/android/client-side/ad-rules \"View this page for the Android platform docs.\") [iOS](/interactive-media-ads/docs/sdks/ios/client-side/ad-rules \"View this page for the iOS platform docs.\") [tvOS](/interactive-media-ads/docs/sdks/tvos/client-side/ad-rules \"View this page for the tvOS platform docs.\")\n\nIMA HTML5 SDK supports fully automated ad playlists. This feature\ninserts ad breaks into the content as specified in\n[Google Ad Manager](//www.google.com/dfp)\nwhen trafficking your ads. It also greatly simplifies the video player code\nnecessary to support ad breaks, including pre-rolls, mid-rolls and post-rolls.\n\n- When creating the `AdsManager`, a `contentPlayback` object is passed in using the [getAdsManager](/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdsManagerLoadedEvent#google.ima.AdsManagerLoadedEvent.getAdsManager) call. This object must have a `currentTime` property that returns the current playhead position of the video. If you're using an HTML5 `video` element to display your content, you can just pass that element to the SDK. This object is used to track the progress of the content playback so ad breaks are automatically inserted at the times specified in Ad Manager. You also need to let the SDK know that you want it to handle content state on your behalf. \n\n ```javascript\n var adsRenderingSettings = new google.ima.AdsRenderingSettings();\n adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;\n adsManager = adsManagerLoadedEvent.getAdsManager(\n videoContent, adsRenderingSettings); // See API reference for /interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdsManagerLoadedEvent#google.ima.AdsManagerLoadedEvent.getAdsManager.\n ```\n- To ensure post-rolls are played, you need to let the SDK know when your content is finished. This is a bit tricky, because in some cases the SDK uses your video player to play ads, so you need to make sure you're only letting the SDK know when your content is finished, and not when an ad is finished. You can do that using the following code: \n\n ```javascript\n var videoContent = document.getElementById('contentElement');\n var contentEndedListener = function() {adsLoader.contentComplete();};\n\n videoContent.addEventListener('ended', contentEndedListener);\n\n function onContentPauseRequested() {\n contentElement.removeEventListener('ended', contentEndedListener);\n ...\n }\n\n function onContentResumeRequested() {\n contentElement.addEventListener('ended', contentEndedListener);\n ...\n }\n ```\n- The [CONTENT_PAUSE_REQUESTED](/interactive-media-ads/docs/sdks/html5/client-side/reference/namespace/google.ima.AdEvent#google.ima.AdEvent.Type) and [CONTENT_RESUME_REQUESTED](/interactive-media-ads/docs/sdks/html5/client-side/reference/namespace/google.ima.AdEvent#google.ima.AdEvent.Type) events are used to pause and resume the content when ad breaks are played.\n- If your video player supports drag-to-seek, and the current time property of the video player updates while the user is dragging, the SDK can't differentiate between content progressing normally and a user seeking through the content. You must use a custom contentPlayback object as your parameter to `getAdsManager`. For an example of this use case, see [The Trouble with Seeking](#trouble-with-seeking).\n\n**Note:** When the content has finished playing or\nthe user has stopped playback, be sure to call AdsLoader.contentComplete\nin order to signal to the SDK that the content is done. The SDK then plays\nthe post-roll ad break, if one has been scheduled. The `ALL_ADS_COMPLETED`\nevent is raised when ALL ad breaks have been played. In addition, note\nthat content tracking begins when `init()` is called and you should\nalways call `init()` before playing content.\n\nDisable automatic playback of ad breaks\n\nIn some circumstances you may want to prevent the SDK from playing ad breaks until\nyou're ready for them. In this scenario, you can disable automatic playback of ad breaks\nin favor of letting the SDK know when you're ready for an ad break to play. With this\nconfiguration, once the SDK has loaded an ad break, it fires an\n`AD_BREAK_READY` event. When your player is ready for the ad break to start,\nyou can call adsManager.start(): \n\n```javascript\nfunction requestAds() {}\n ...\n adsLoader.getSettings().setAutoPlayAdBreaks(false);\n ...\n}\n\nfunction onAdsManagerLoaded() {\n ...\n // For non-auto ad breaks, listen for ad break ready\n adsManager.addEventListener(\n google.ima.AdEvent.Type.AD_BREAK_READY,\n adBreakReadyHandler);\n ...\n}\n\nfunction adBreakReadyHandler() {\n // Once we're ready to play ads. To skip this ad break, simply return\n // from this handler without calling adsManager.start().\n adsManager.start();\n}\n```\n\nTry it out\n\nSee the following code for a working implementation.\nSee the Pen \\\u003ca href='http://codepen.io/imasdk/pen/QyBNrq/'\\\u003eManual Ad Breaks\\\u003c/a\\\u003e by IMA SDK (\\\u003ca href='http://codepen.io/imasdk'\\\u003e@imasdk\\\u003c/a\\\u003e) on \\\u003ca href='http://codepen.io'\\\u003eCodePen\\\u003c/a\\\u003e.\n\nThe trouble with seeking\n\nIf you use ad rules, you may run into a problem with click-and-drag seeking.\nSpecifically, after a user clicks and drags to seek through video past multiple\nmidroll pods, they may see 2 or more of those pods play back to back before\ncontent resumes. This is caused by the video playhead time updating while the\nuser is seeking; if the SDK happens to poll for the current time while the user\nseeks past an ad, it may think that ad should be played. When the content\nresumes, it plays that ad, and then the most recent ad since the seek. For a\nvisual representation of this problem, see the following diagram:\n\nSave the current time when the user starts seeking, and report that time when the SDK\nasks for it until the user resumes normal playback. For a visual representation of this\nsolution, see the following diagram:\n\nWith this solution, you properly skip the 0:10 mid-roll and only play the 0:20 mid-roll.\nThis is done using a custom playhead tracker in the following code snippet. This code\ncontains modifications (shown in bold) of `ads.js` in the advanced HTML5\nsample available on our\n[download page](/interactive-media-ads/docs/sdks/html5/download). \n\n```javascript\nvar Ads = function(application, videoPlayer) {\n ...\n this.currentTime = 0;\n setInterval(this.updateCurrentTime, 1000);\n};\n\nAds.prototype.updateCurrentTime = function() {\n if (!this.videoPlayer_.contentPlayer.seeking) {\n this.currentTime = this.videoPlayer_.contentPlayer.currentTime;\n }\n};\n\nAds.prototype.onAdsManagerLoaded_ = function(adsManagerLoadedEvent) {\n this.application_.log('Ads loaded.');\n this.adsManager_ = adsManagerLoadedEvent.getAdsManager(this);\n this.processAdsManager_(this.adsManager_);\n};\n```\n\nKnown issues with mobile Safari\n\nThis method should work on every plaform except mobile Safari. On mobile\nSafari, the seeking property of the video tag is not properly implemented (it\nalways returns false). To get around that, you need to do your own check to\nsee if the user is seeking through the video. The sample code for this method\nfollows. Again, the bolded lines are modifications to existing code. \n\n```javascript\nvar Ads = function(application, videoPlayer) {\n ...\n this.currentTime = 0;\n setInterval(this.updateCurrentTime, 1000);\n this.seeking = false;\n this.seekCheckInterval = 1000;\n // You may need to adjust this value, depending on your platform\n this.seekThreshold = 100;\n this.previousTime = 0;\n setInterval(\n Application.bind(this, this.checkForSeeking),\n this.seekCheckInterval);\n};\n\nAds.prototype.updateCurrentTime = function() {\n if (!this.seeking) {\n this.currentTime = this.videoPlayer_.contentPlayer.currentTime;\n }\n};\n\nAds.prototype.checkForSeeking = function() {\n var currentTime = this.videoPlayer_.contentPlayer.currentTime;\n // How much time has passed since you last ran this method, in milliseconds\n var diff = (currentTime - this.previousTime) * 1000;\n // If that difference is greater than the time since you last ran this method,\n // plus the threshold, the user was seeking\n if (Math.abs(diff) \u003e this.interval + this.threshold) {\n this.seeking = true;\n } else {\n this.seeking = false;\n }\n // Grab the current video time again to make up for time spent in this method\n previousTime = this.videoPlayer_.contentPlayer.currentTime;\n};\n\nAds.prototype.onAdsManagerLoaded_ = function(adsManagerLoadedEvent) {\n this.application_.log('Ads loaded.');\n this.adsManager_ = adsManagerLoadedEvent.getAdsManager(this);\n this.processAdsManager_(this.adsManager_);\n};\n```\n\nWith these changes, the SDK is now using the currentTime property of your `Ads`\nobject to determine when to play ad breaks, not the `currentTime` property of the\ncontent video player."]]