搭配 Android TV 使用 Android 專用 IMA SDK

建議您採用下列幾項最佳做法,確保使用 Android IMA SDK 時,應用程式能夠在 Android TV 上正常運作。

請先熟悉開發 Android 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;