我们建议您遵循几项最佳实践,以确保您的应用在使用 IMA SDK for Android 时在 Android TV 上正常运行。
首先,请熟悉如何为 Android 开发 TV 应用。具体而言,请确保您的 Activity 已针对 TV 进行设置,如 入门指南中所述。您 还需要处理 TV 导航,以 确保用户可以在 Android TV 上顺利浏览您的应用。
处理可跳过的广告
该 SDK 会针对类似 TV 的设备优化可跳过的广告格式,例如 移除与 了解详情 按钮互动的功能。默认情况下,当可以跳过广告时,该 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;