搭配 Android TV 使用 Android 專用 IMA SDK

建議您使用數種最佳做法,確保使用 Android 版 IMA SDK 時,您的應用程式能在 Android TV 上正常運作。

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