針對 Android TV 進行最佳化調整

使用 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 圖示備用圖片周圍的廣告播放。如要查看如何處理這項作業的範例,請參閱進階範例中的下列程式碼片段。

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;