请求在纯音频内容中投放广告
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
IMA HTML5 SDK 支持音频广告,其设置与视频广告类似,但也存在一些关键区别。如需了解本指南未涵盖的 IMA 设置的任何部分,请参阅 HTML5 入门指南。
使用 <audio> 标记进行内容播放
AdDisplayContainer
的构造函数接受一个名为 videoElement
的第二个参数,IMA 会将其作为内容播放器进行跟踪。此参数接受 <video>
或 <audio>
标记。对于音频内容和广告,本指南建议使用 <audio>
标记构建 AdDisplayContainer
。如果您有视频内容,可以使用 <video>
代码混合展示音频广告和视频广告:
index.html
<audio id="audio-player"></audio>
<div class="ad-container"></div>
ads.js
audioPlayer = document.getElementById('audio-player');
adContainer = document.getElementById('ad-container');
adDisplayContainer = new google.ima.AdDisplayContainer(adContainer,
audioPlayer);
隐藏 AdDisplayContainer
即使广告或内容没有显示部分,IMA HTML5 SDK 仍需要 AdDisplayContainer
。因此,我们建议隐藏 AdDisplayContainer
。请参阅以下示例,了解如何隐藏元素:
style.css
.ad-container {
display: none;
}
自定义控件
由于 AdDisplayContainer
处于隐藏状态,因此需要使用自定义控件来处理广告插播期间的播放。AdsManager
提供了一些方法,可用于实现这些自定义控件:
处理可跳过的广告
由于没有可见的 AdDisplayContainer
,IMA SDK 无法显示跳过广告按钮。如需处理可跳过的广告,请实现以下 IMA 方法:
以下示例代码演示了如何执行此操作。
ads.js
您可以设置 updateSkippable
函数,以确定广告是否可以跳过以及何时可以跳过。应在每个 AD_PROGRESS
IMA 事件中调用此函数。如需了解如何为 IMA 事件设置监听器,请参阅入门指南。
function onAdsManagerLoaded(adsManagerLoadedEvent) {
adsManager = adsManagerLoadedEvent.getAdsManager(
audioPlayer);
...
adsManager.addEventListener(
google.ima.AdEvent.Type.AD_PROGRESS,
onAdEvent);
...
}
function onAdEvent(adEvent) {
const ad = adEvent.getAd();
if (ad) {
currentAd = ad; // currentAd is defined outside of this functions scope.
}
switch (adEvent.type) {
...
case google.ima.AdEvent.Type.AD_PROGRESS:
// See https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdProgressData
const adProgressData = adEvent.getAdData();
updateSkippable(
adProgressData.currentTime,
currentAd.getSkipTimeOffset()
);
break;
...
}
}
/**
* Called when there may be a change in the skippable state.
* @param {number} currentTime The current time of the
* currently playing ad.
* @param {number} skipTimeOffset The number of seconds of playback
* before the ad becomes skippable. -1 is returned for non skippable
* ads or if this is unavailable.
*/
updateSkippable(currentTime, skipTimeOffset) {
const isAdSkippable = skipTimeOffset !== -1;
const isSkipCurrentlyAllowed = adsManager.getAdSkippableState();
const timeTillSkipInSeconds = Math.ceil(skipTimeOffset - currentTime);
updateSkipUI(
isAdSkippable, isSkipCurrentlyAllowed, timeTillSkipInSeconds);
}
与视频广告不同,IMA 无法为音频广告提供跳过按钮。
开发者必须为可跳过的广告添加自定义界面,这可以通过简单的 <button>
标记来完成。此 updateSkipUI
函数会根据广告是否可跳过、广告当前是否可跳过以及广告变为可跳过所剩余的时间来更新跳过按钮。它使用 IMA 未提供的 '.hidden'
类。.hidden
类会向 <button>
添加 display: none;
。
/**
* Updates the skip button UI.
* @param {boolean} isAdSkippable if the current ad is a skippable ad.
* @param {boolean} isSkipCurrentlyAllowed if the ad can be skipped now.
* @param {number} timeTillSkipInSeconds time until the ad can be skipped in
* seconds.
*/
updateSkipUI(isAdSkippable, isSkipCurrentlyAllowed, timeTillSkipInSeconds) {
if (isAdSkippable) {
skipButton.classList.remove('hidden');
if (isSkipCurrentlyAllowed) {
skipButton.textContent = 'Skip ad';
skipButton.disabled = false;
} else {
skipButton.textContent = `Skip in ${timeTillSkipInSeconds} seconds`;
skipButton.disabled = true;
}
} else {
skipButton.classList.add('hidden');
}
}
最后,为用户点击自定义跳过按钮设置监听器。如需跳过广告,请调用 adsManager.skip()
函数。
skipButton.addEventListener('click', () => {
adsManager.skip();
});
以下是使用音频广告设置 IMA SDK 时需要进行的主要更改。如需了解实现问题的解答,请访问 IMA SDK 技术论坛。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-01。
[null,null,["最后更新时间 (UTC):2025-08-01。"],[[["\u003cp\u003eThe IMA HTML5 SDK allows you to integrate audio ads into your web application using an \u003ccode\u003e<audio>\u003c/code\u003e tag for playback and an \u003ccode\u003eAdDisplayContainer\u003c/code\u003e for ad management, similar to video ads but with some key distinctions.\u003c/p\u003e\n"],["\u003cp\u003eYou should hide the \u003ccode\u003eAdDisplayContainer\u003c/code\u003e element because audio ads don't have a visual component, but it is still required by the SDK.\u003c/p\u003e\n"],["\u003cp\u003eSince the \u003ccode\u003eAdDisplayContainer\u003c/code\u003e is hidden, you need to implement custom controls using \u003ccode\u003eAdsManager\u003c/code\u003e methods like \u003ccode\u003epause()\u003c/code\u003e, \u003ccode\u003eresume()\u003c/code\u003e, and \u003ccode\u003esetVolume()\u003c/code\u003e to manage audio playback during ad breaks.\u003c/p\u003e\n"],["\u003cp\u003eFor skippable audio ads, you need to create a custom skip button and use IMA methods like \u003ccode\u003egetAdSkippableState()\u003c/code\u003e, \u003ccode\u003egetAdData()\u003c/code\u003e, and \u003ccode\u003egetSkipTimeOffset()\u003c/code\u003e along with event listeners to manage its behavior, as the SDK doesn't provide a built-in skip button for audio.\u003c/p\u003e\n"]]],[],null,["# Request ads on audio-only content\n\nThe IMA HTML5 SDK supports audio ads with a similar setup as video ads but with\na few key differences. For any part of the IMA setup not covered in this guide,\nsee the [HTML5 Get started\nguide](/interactive-media-ads/docs/sdks/html5/client-side).\n\nUse of an \\\u003caudio\\\u003e tag for content playback\n--------------------------------------------\n\nThe constructor for\n[`AdDisplayContainer`](/interactive-media-ads/docs/sdks/html5/client-side/reference/class/google.ima.AdDisplayContainer#google.ima.AdDisplayContainer.constructor)\ntakes a second argument named `videoElement` which IMA tracks as the content\nplayer. This argument accepts both a `\u003cvideo\u003e` or `\u003caudio\u003e` tag. For audio\ncontent and ads, this guide recommends using an `\u003caudio\u003e` tag to construct an\n`AdDisplayContainer`. If you have video content, you can use a `\u003cvideo\u003e` tag for\nshowing a mix of audio and video ads:\n\n#### index.html\n\n \u003caudio id=\"audio-player\"\u003e\u003c/audio\u003e\n \u003cdiv class=\"ad-container\"\u003e\u003c/div\u003e\n\n#### ads.js\n\n audioPlayer = document.getElementById('audio-player');\n adContainer = document.getElementById('ad-container');\n adDisplayContainer = new google.ima.AdDisplayContainer(adContainer,\n audioPlayer);\n\nHide the AdDisplayContainer\n---------------------------\n\nThe IMA HTML5 SDK still requires an `AdDisplayContainer` even if there is no\ndisplay portion to the ads or content. For this reason, we recommend hiding the\n`AdDisplayContainer`. See an example of how you can hide the element:\n\n#### style.css\n\n .ad-container {\n display: none;\n }\n\nCustom controls\n---------------\n\nBecause the `AdDisplayContainer` is hidden, custom controls are needed to handle\nplayback during ad breaks. `AdsManager` has methods that can be used to\nimplement these custom controls:\n\n- [`pause()`](/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdsManager#google.ima.AdsManager.pause)\n- [`resume()`](/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdsManager#google.ima.AdsManager.resume)\n- [`setVolume()`](/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdsManager#google.ima.AdsManager.setVolume)\n\nHandle skippable ads\n--------------------\n\nBecause there is no visible `AdDisplayContainer`, the IMA SDK cannot show a\n**Skip ad** button. To handle skippable ads, implement the following IMA\nmethods:\n\n- [`AdsManager.getAdSkippableState()`](/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdsManager#google.ima.AdsManager.getAdSkippableState)\n- [`AdEvent.getAdData()`](/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdEvent#google.ima.AdEvent.getAdData)\n- [`Ad.getSkipTimeOffset()`](/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.Ad#google.ima.Ad.getSkipTimeOffset)\n\nThe following sample code demonstrates how to do this.\n\n#### ads.js\n\nYou can set up an `updateSkippable` function to determine whether and when an ad\ncan be skipped. This function should be called on each `AD_PROGRESS` IMA event.\nSee the\n[Getting started guide](/interactive-media-ads/docs/sdks/html5/client-side#triggering-play-and-pause-events)\nfor directions on how to set up listeners for IMA events. \n\n function onAdsManagerLoaded(adsManagerLoadedEvent) {\n adsManager = adsManagerLoadedEvent.getAdsManager(\n audioPlayer);\n\n ...\n\n adsManager.addEventListener(\n google.ima.AdEvent.Type.AD_PROGRESS,\n onAdEvent);\n\n ...\n\n }\n\n function onAdEvent(adEvent) {\n const ad = adEvent.getAd();\n if (ad) {\n currentAd = ad; // currentAd is defined outside of this functions scope.\n }\n switch (adEvent.type) {\n\n ...\n\n case google.ima.AdEvent.Type.AD_PROGRESS:\n // See https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdProgressData\n const adProgressData = adEvent.getAdData();\n\n updateSkippable(\n adProgressData.currentTime,\n currentAd.getSkipTimeOffset()\n );\n break;\n ...\n\n }\n }\n\n /**\n * Called when there may be a change in the skippable state.\n * @param {number} currentTime The current time of the\n * currently playing ad.\n * @param {number} skipTimeOffset The number of seconds of playback\n * before the ad becomes skippable. -1 is returned for non skippable\n * ads or if this is unavailable.\n */\n updateSkippable(currentTime, skipTimeOffset) {\n const isAdSkippable = skipTimeOffset !== -1;\n const isSkipCurrentlyAllowed = adsManager.getAdSkippableState();\n const timeTillSkipInSeconds = Math.ceil(skipTimeOffset - currentTime);\n updateSkipUI(\n isAdSkippable, isSkipCurrentlyAllowed, timeTillSkipInSeconds);\n }\n\nUnlike with video ads, IMA is not able to provide a skip button for audio ads.\nDevelopers must add custom UI for skippable ads, which can be done with a\nsimple `\u003cbutton\u003e` tag. This `updateSkipUI` function updates the skip button\nbased on if the ad is skippable, if the ad is currently skippable, and\nhow much time is remaining until the ad becomes skippable. It makes use of the\n`'.hidden'` class, which is not provided by IMA. The `.hidden` class adds\n`display: none;` to the `\u003cbutton\u003e`. \n\n /**\n * Updates the skip button UI.\n * @param {boolean} isAdSkippable if the current ad is a skippable ad.\n * @param {boolean} isSkipCurrentlyAllowed if the ad can be skipped now.\n * @param {number} timeTillSkipInSeconds time until the ad can be skipped in\n * seconds.\n */\n updateSkipUI(isAdSkippable, isSkipCurrentlyAllowed, timeTillSkipInSeconds) {\n if (isAdSkippable) {\n skipButton.classList.remove('hidden');\n if (isSkipCurrentlyAllowed) {\n skipButton.textContent = 'Skip ad';\n skipButton.disabled = false;\n } else {\n skipButton.textContent = `Skip in ${timeTillSkipInSeconds} seconds`;\n skipButton.disabled = true;\n }\n } else {\n skipButton.classList.add('hidden');\n }\n }\n\nFinally, set a listener for user clicks on your custom skip button. To skip ads,\ncall the\n[`adsManager.skip()`](/interactive-media-ads/docs/sdks/html5/client-side/reference/interface/google.ima.AdsManager#google.ima.AdsManager.skip)\nfunction. \n\n skipButton.addEventListener('click', () =\u003e {\n adsManager.skip();\n });\n\nThese are the main changes needed to set up the IMA SDK with audio ads. For\nanswers to implementation issues, visit the\n[IMA SDK Technical Forum](//groups.google.com/g/ima-sdk)."]]