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

我们建议您遵循以下最佳实践,以确保在使用 IMA SDK for Android 时,您的应用能够在 Android TV 上正常运行。

首先,请熟悉面向 Android 开发 TV 应用。具体而言,请确保您的 activity 已按照入门指南中的说明设置为适用于 TV。您还需要处理 TV 导航,以确保用户能够在 Android TV 上顺畅浏览您的应用。

处理可跳过的广告

该 SDK 针对类似电视的设备优化了可跳过的广告格式,例如取消了与了解详情按钮互动的功能。默认情况下,当可跳过功能可用时,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;