建議您採用下列最佳做法,確保應用程式在 Android TV 上使用 IMA SDK for Android 時能正常運作。
請先熟悉開發 Android TV 應用程式。具體來說,請務必按照入門指南中的說明,為電視設定 Activity。您也需要處理電視導覽,確保使用者能在 Android TV 上順利瀏覽應用程式。
處理可略過的廣告
SDK 會針對電視等裝置最佳化可略過格式,例如移除與「瞭解詳情」按鈕互動的功能。根據預設,SDK 會在可略過廣告時將焦點設在略過按鈕上,以便在 Android TV 上略過廣告。因此,您不需要額外設定,就能支援可略過的廣告。
您可以呼叫 AdsRenderingSettings.setFocusSkipButtonWhenAvailable() 來設定這項功能。
如要進一步瞭解支援的廣告,請參閱相容性矩陣。
處理 VAST 圖示備用圖片
IMA SDK 會偵測、顯示及處理使用者與 VAST 圖示備用圖片的互動。應用程式應監聽 ICON_TAPPED 和 ICON_FALLBACK_IMAGE_CLOSED 事件,處理使用「為什麼會顯示這則廣告?」(WTA) 的廣告播放。
新增布林值,追蹤是否顯示 VAST 圖示備用圖片。接著,監聽 ICON_TAPPED 和 ICON_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;