IMA SDK for Android を使用する際に、Android TV でアプリが正しく動作するようにするためのベスト プラクティスをいくつかご紹介します。
まず、Android 向け TV アプリの開発についてご確認ください。特に、スタートガイドで説明されているように、アクティビティが TV 向けに設定されていることを確認してください。また、テレビのナビゲーションを処理して、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;