我们建议您遵循以下最佳实践,以确保在使用 IMA SDK for Android 时,您的应用能够在 Android TV 上正常运行。
首先,请熟悉面向 Android 开发 TV 应用。具体而言,请确保您的 activity 已按照入门指南中的说明设置为适用于 TV。您还需要处理 TV 导航,以确保用户能够在 Android TV 上顺畅浏览您的应用。
处理可跳过的广告
该 SDK 会针对电视类设备优化可跳过的广告格式,例如移除了解详情按钮。默认情况下,SDK 会在可跳过时将焦点设置为跳过按钮,以便在 Android TV 上跳过广告。因此,您无需执行任何额外的操作即可支持可跳过式广告。
您可以通过调用 AdsRenderingSettings.setFocusSkipButtonWhenAvailable()
来进行配置。
如需详细了解支持哪些广告,请参阅兼容性矩阵。
处理 VAST 图标后备图片
IMA SDK 会检测、呈现和处理用户与 VAST 图标后备图片的互动。您的应用应监听 ICON_TAPPED
和 ICON_FALLBACK_IMAGE_CLOSED
事件,以处理使用“为何展示此广告”(WTA) 的广告的广告播放。
添加一个布尔值,用于跟踪 VAST 图标后备图片是否显示。然后,监听 ICON_TAPPED
和 ICON_FALLBACK_IMAGE_CLOSED
以处理 VAST 图标后备图片周围的广告播放。如需查看如何在高级示例中处理此问题的示例,请参阅以下代码段。
VideoPlayerController.java
// Copyright 2014 Google Inc. All Rights Reserved.
package com.google.ads.interactivemedia.v3.samples.videoplayerapp;
import android.app.UiModeManager;
import android.content.Context;
...
// Tracks if the SDK is playing an ad, since the SDK might not necessarily use
// the video player provided to play the video ad.
private boolean isAdPlaying;
// Tracks whether the SDK has a VAST icon fallback image showing.
private boolean isConnectedTvFallbackImageShowing = false;
// View that handles taps to toggle ad pause/resume during video playback.
private View playPauseToggle;
// View that we can write log messages to, to display in the UI.
private Logger log;
...
adsManager.addAdEventListener(
new AdEvent.AdEventListener() {
/** Responds to AdEvents. */
@Override
public void onAdEvent(AdEvent adEvent) {
...
case CONTENT_RESUME_REQUESTED:
// AdEventType.CONTENT_RESUME_REQUESTED is fired when the ad is
// completed and you should start playing your content.
resumeContent();
break;
case ICON_TAPPED:
// The user has tapped a VAST icon fallback image. On Android
// mobile apps, the SDK will navigate to the landing page. On
// Connected TV devices, the SDK will present a modal dialog
// containing the VAST icon fallback image.
// Check if the app is running on a TV device.
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
isConnectedTvFallbackImageShowing = true;
}
// Focus the IMA WebView for easier access to ad UI elements.
adsManager.focus();
break;
case PAUSED:
if (isConnectedTvFallbackImageShowing) {
// Do not show the controls; continue to leave the controls in
// the hands of the ads SDK.
break;
}
isAdPlaying = false;
videoPlayerWithAdPlayback.enableControls();
break;
case ICON_FALLBACK_IMAGE_CLOSED:
// The user has closed the VAST icon fallback image. This may
// be a good time to resume ad playback if the user is ready to
// continue playing the ad. This event only fires for Connected
// TV devices.
isConnectedTvFallbackImageShowing = false;
adsManager.resume();
break;
case RESUMED:
isAdPlaying = true;
videoPlayerWithAdPlayback.disableControls();
break;