我们建议您遵循以下几项最佳实践,以确保应用在使用 IMA SDK for Android 时能在 Android TV 上正常运行。
首先,请熟悉为 Android 开发 TV 应用。具体来说,请确保您的 Activity 已针对电视进行设置,如入门指南中所述。您还需要处理 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;