将 IMA SDK for Android 与 Android TV 结合使用

我们建议您遵循以下几项最佳做法,以确保使用 IMA SDK for Android 时您的应用可在 Android TV 上正常运行。

首先,熟悉开发 Android TV 应用。具体而言,请确保按照入门指南中的说明针对 TV 设置 activity。此外,您还需要处理 TV 导航,确保用户可以在 Android TV 上顺畅导航您的应用。

处理可跳过式广告

SDK 针对类似 TV 的设备优化了可跳过式广告格式,例如移除了与了解详情按钮互动的功能。默认情况下,当“跳过”按钮可用时,SDK 会将焦点设置为“跳过”按钮,以便用户在 Android TV 上跳过广告。因此,无需执行额外的操作即可支持可跳过的广告。

您可以通过调用 AdsRenderingSettings.setFocusSkipButtonWhenAvailable() 来配置此项。

如需详细了解支持的广告,请查看兼容性矩阵

处理 VAST 图标后备图片

IMA SDK 会检测、呈现并处理用户与 VAST 图标后备图片的互动情况。您的应用应监听 ICON_TAPPEDICON_FALLBACK_IMAGE_CLOSED 事件,以处理使用“为什么显示该广告”(WTA) 的广告的播放。

添加一个布尔值,以跟踪是否展示 VAST 图标后备图片。然后,监听 ICON_TAPPEDICON_FALLBACK_IMAGE_CLOSED 以处理围绕 VAST 图标后备图片的广告播放。请参阅以下代码段,通过示例了解高级示例中如何处理此过程。

app/src/main/java/com/google/ads/interactivemedia/v3/samples/videoplayerapp/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;