Android TV で Android 向け IMA SDK を使用する

IMA SDK for Android を使用する場合は、Android TV でアプリが正しく機能するように、いくつかのベスト プラクティスをおすすめします。

まず、Android 向け TV アプリの開発について学習します。特に、スタートガイドで説明されているように、アクティビティがテレビ用に設定されていることを確認してください。また、テレビのナビゲーションを処理して、ユーザーが Android TV でアプリを適切に操作できるようにする必要があります。

スキップ可能な広告を処理する

この SDK は、テレビのようなデバイス向けにスキップ可能なフォーマットを最適化します。たとえば、[詳細] ボタンを操作する機能を削除します。デフォルトでは、スキップが可能な場合は SDK がスキップボタンにフォーカスを設定するため、Android TV で広告をスキップできます。そのため、スキップ可能広告をサポートするために追加の作業を行う必要はありません。

これは、AdsRenderingSettings.setFocusSkipButtonWhenAvailable() を呼び出して構成できます。

サポートされている広告の詳細については、互換性マトリックスをご覧ください。

VAST アイコンのフォールバック イメージを処理する

IMA SDK は、VAST アイコンのフォールバック画像に対するユーザー操作を検出してレンダリングし、処理します。アプリは、ICON_TAPPED イベントと ICON_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;