前提条件
- Google Mobile Ads SDK がインポート済みであること(単独または Firebase の一部として)
- ネイティブ広告フォーマットを統合していること
MediaContent
ネイティブ広告 では、動画や画像といったメディア コンテンツに関する情報を取得するためのMediaContent
オブジェクトを使用することができます。このオブジェクトは、動画広告の再生をコントロールし、再生イベントをリッスンするためにも使用されます。MediaContent
オブジェクトを取得するには、NativeAd.getMediaContent()
を呼び出します。
MediaContent
オブジェクトには、動画のアスペクト比や再生時間などの情報が含まれます。以下のスニペットは、ネイティブ広告のアスペクト比と再生時間を取得する方法を示しています。
Java
if (myNativeAd.getMediaContent().hasVideoContent()) { float mediaAspectRatio = myNativeAd.getMediaContent().getAspectRatio(); float duration = myNativeAd.getMediaContent().getDuration(); ... }
Kotlin
if (myNativeAd.getMediaContent().hasVideoContent()) { val mediaAspectRatio: Float = myNativeAd.getMediaContent().getAspectRatio() val duration: Float = myNativeAd.getMediaContent().getDuration() ... }
VideoOptions
VideoOptions
クラスを使うと、ネイティブ動画アセットの動作をアプリで設定できます。VideoOptions
オブジェクトは、AdLoader
の構成時に使う NativeAdOptions
オブジェクトに割り当てる必要があります。次の例をご覧ください。
Java
VideoOptions videoOptions = new VideoOptions.Builder() .setStartMuted(false) .build(); NativeAdOptions adOptions = new NativeAdOptions.Builder() .setVideoOptions(videoOptions) .build(); AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") .forNativeAd( ... ) .withNativeAdOptions(adOptions) .build();
Kotlin
val videoOptions = VideoOptions.Builder() .setStartMuted(false) .build() val adOptions = NativeAdOptions.Builder() .setVideoOptions(videoOptions) .build() val adLoader = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") .forNativeAd( ... ) .withNativeAdOptions(adOptions) .build()
動画イベントのコールバック
特定の動画イベントを処理するには、抽象 VideoLifecycleCallbacks
クラスを拡張するクラスを作成し、VideoController
で setVideoLifecycleCallbacks()
を呼び出します。次に、目的のコールバックのみをオーバーライドします。
Java
myNativeAd.getMediaContent().getVideoController() .setVideoLifecycleCallbacks(new VideoLifecycleCallbacks() { /** Called when video playback first begins. */ @Override public void onVideoStart() { // Do something when the video starts the first time. Log.d("MyApp", "Video Started"); } /** Called when video playback is playing. */ @Override public void onVideoPlay() { // Do something when the video plays. Log.d("MyApp", "Video Played"); } /** Called when video playback is paused. */ @Override public void onVideoPause() { // Do something when the video pauses. Log.d("MyApp", "Video Paused"); } /** Called when video playback finishes playing. */ @Override public void onVideoEnd() { // Do something when the video ends. Log.d("MyApp", "Video Ended"); } /** Called when the video changes mute state. */ @Override public void onVideoMute(boolean isMuted) { // Do something when the video is muted. Log.d("MyApp", "Video Muted"); } });
Kotlin
myNativeAd.getMediaContent().getVideoController().setVideoLifecycleCallbacks { /** Called when video playback first begins. */ override fun onVideoStart() { // Do something when the video starts the first time. Log.d("MyApp", "Video Started") } /** Called when video playback is playing. */ override fun onVideoPlay() { // Do something when the video plays. Log.d("MyApp", "Video Played") } /** Called when video playback is paused. */ override fun onVideoPause() { // Do something when the video pauses. Log.d("MyApp", "Video Paused") } /** Called when video playback finishes playing. */ override fun onVideoEnd() { // Do something when the video ends. Log.d("MyApp", "Video Ended") } /** Called when the video changes mute state. */ override fun onVideoMute(boolean isMuted) { // Do something when the video is muted. Log.d("MyApp", "Video Muted") } }