From b49289359b380167a1567ff6f886008a03722ab6 Mon Sep 17 00:00:00 2001 From: Adrien Thiery Date: Fri, 5 Jan 2018 16:09:46 +0100 Subject: [PATCH] Enabled preroll ads on Android --- .../brightcove-video/android/build.gradle | 11 +-- .../BrightcovePlayerView.java | 85 ++++++++++++++++++- .../rntbrightcovevideo/RNTBrightcoveView.java | 14 ++- 3 files changed, 102 insertions(+), 8 deletions(-) diff --git a/packages/brightcove-video/android/build.gradle b/packages/brightcove-video/android/build.gradle index fa4f16ca48a..e52395bcb1b 100644 --- a/packages/brightcove-video/android/build.gradle +++ b/packages/brightcove-video/android/build.gradle @@ -19,12 +19,13 @@ android { } repositories { - maven { - url "http://repo.brightcove.com/releases" - } + maven { url "http://repo.brightcove.com/releases" } + maven { url "https://maven.google.com" } } dependencies { - implementation "com.facebook.react:react-native:0.49.3" - api "com.brightcove.player:exoplayer:5.2.0" + implementation "com.facebook.react:react-native:+" + api "com.brightcove.player:exoplayer:6.0.1" + api "com.brightcove.player:android-ima-plugin:6.0.1" + api "com.google.ads.interactivemedia.v3:interactivemedia:3.+" } diff --git a/packages/brightcove-video/android/src/main/java/uk/co/news/rntbrightcovevideo/BrightcovePlayerView.java b/packages/brightcove-video/android/src/main/java/uk/co/news/rntbrightcovevideo/BrightcovePlayerView.java index 8a134f615bb..c5cb5c4fe74 100644 --- a/packages/brightcove-video/android/src/main/java/uk/co/news/rntbrightcovevideo/BrightcovePlayerView.java +++ b/packages/brightcove-video/android/src/main/java/uk/co/news/rntbrightcovevideo/BrightcovePlayerView.java @@ -1,5 +1,7 @@ package uk.co.news.rntbrightcovevideo; +import java.util.ArrayList; + import android.content.Context; import android.graphics.Color; import android.support.annotation.NonNull; @@ -7,6 +9,9 @@ import android.view.SurfaceView; import android.view.View; +import com.brightcove.ima.GoogleIMAComponent; +import com.brightcove.ima.GoogleIMAEventType; + import com.brightcove.player.edge.Catalog; import com.brightcove.player.edge.VideoListener; import com.brightcove.player.event.Event; @@ -17,6 +22,11 @@ import com.brightcove.player.model.Video; import com.brightcove.player.view.BrightcoveExoPlayerVideoView; +import com.google.ads.interactivemedia.v3.api.AdDisplayContainer; +import com.google.ads.interactivemedia.v3.api.AdsManager; +import com.google.ads.interactivemedia.v3.api.AdsRequest; +import com.google.ads.interactivemedia.v3.api.ImaSdkFactory; + public class BrightcovePlayerView extends BrightcoveExoPlayerVideoView { public static final String TAG = BrightcovePlayerView.class.getSimpleName(); @@ -24,6 +34,7 @@ public class BrightcovePlayerView extends BrightcoveExoPlayerVideoView { private Boolean mAutoplay; private Boolean mIsPlaying = false; private Boolean mIsFullscreen = false; + private GoogleIMAComponent googleIMAComponent; private float mProgress = 0; public BrightcovePlayerView(final Context context) { @@ -98,13 +109,15 @@ private void bubbleState(Boolean isPlaying, int headPos) { } } - public void initVideo(String videoId, String accountId, String policyKey, Boolean autoplay, Boolean isFullscreenButtonHidden) { + public void initVideo(String videoId, String accountId, String policyKey, Boolean autoplay, Boolean isFullscreenButtonHidden, String vastTag) { View fullScreenButton = this.findViewById(com.brightcove.player.R.id.full_screen); fullScreenButton.setVisibility(isFullscreenButtonHidden ? View.GONE : View.VISIBLE); mAutoplay = autoplay; + Log.i(TAG, "INIT"); EventEmitter eventEmitter = setupEventEmitter(); + setupGoogleIMA(vastTag, eventEmitter); Catalog catalog = new Catalog(eventEmitter, accountId, policyKey); catalog.findVideoByID(videoId, createVideoListener()); @@ -175,4 +188,74 @@ public float getPlayheadPosition() { return playheadPosition; } + /** + * Setup the Brightcove IMA Plugin. + */ + private void setupGoogleIMA(final String adRulesURL, final EventEmitter eventEmitter) { + Log.i(TAG, "SETUP"); + + // Establish the Google IMA SDK factory instance. + final ImaSdkFactory sdkFactory = ImaSdkFactory.getInstance(); + + // Enable logging up ad start. + eventEmitter.on(EventType.AD_STARTED, new EventListener() { + @Override + public void processEvent(Event event) { + Log.v(TAG, event.getType()); + Log.i(TAG, "STARTED"); + } + }); + + // Enable logging any failed attempts to play an ad. + eventEmitter.on(GoogleIMAEventType.DID_FAIL_TO_PLAY_AD, new EventListener() { + @Override + public void processEvent(Event event) { + Log.i(TAG, "FAILED"); + Log.v(TAG, event.getType()); + } + }); + + // Enable Logging upon ad completion. + eventEmitter.on(EventType.AD_COMPLETED, new EventListener() { + @Override + public void processEvent(Event event) { + Log.v(TAG, event.getType()); + Log.i(TAG, "COMPLETED"); + } + }); + + final BrightcoveExoPlayerVideoView playerView = this; + + // Set up a listener for initializing AdsRequests. The Google + // IMA plugin emits an ad request event as a result of + // initializeAdsRequests() being called. + eventEmitter.on(GoogleIMAEventType.ADS_REQUEST_FOR_VIDEO, new EventListener() { + @Override + public void processEvent(Event event) { + Log.i(TAG, "VIDEO REQUEST"); + // Create a container object for the ads to be presented. + AdDisplayContainer container = sdkFactory.createAdDisplayContainer(); + container.setPlayer(googleIMAComponent.getVideoAdPlayer()); + container.setAdContainer(playerView); + + // Build an ads request object and point it to the ad + // display container created above. + AdsRequest adsRequest = sdkFactory.createAdsRequest(); + adsRequest.setAdTagUrl(adRulesURL); + adsRequest.setAdDisplayContainer(container); + + ArrayList adsRequests = new ArrayList(1); + adsRequests.add(adsRequest); + + // Respond to the event with the new ad requests. + event.properties.put(GoogleIMAComponent.ADS_REQUESTS, adsRequests); + eventEmitter.respond(event); + } + }); + + // Create the Brightcove IMA Plugin and pass in the event + // emitter so that the plugin can integrate with the SDK. + googleIMAComponent = new GoogleIMAComponent(playerView, eventEmitter, true); + } + } diff --git a/packages/brightcove-video/android/src/main/java/uk/co/news/rntbrightcovevideo/RNTBrightcoveView.java b/packages/brightcove-video/android/src/main/java/uk/co/news/rntbrightcovevideo/RNTBrightcoveView.java index d30a60aec0e..ab3c687ee02 100644 --- a/packages/brightcove-video/android/src/main/java/uk/co/news/rntbrightcovevideo/RNTBrightcoveView.java +++ b/packages/brightcove-video/android/src/main/java/uk/co/news/rntbrightcovevideo/RNTBrightcoveView.java @@ -22,6 +22,7 @@ public class RNTBrightcoveView extends FrameLayout { private String mVideoId; private String mAccountId; private String mPolicyKey; + private String mVastTag; private Boolean mAutoplay; private Boolean mHideFullScreenButton; private BrightcovePlayerView mPlayerView; @@ -55,6 +56,13 @@ public void setVideoId(final String videoId) { } } + public void setVastTag(final String vastTag) { + if (mVastTag == null) { + mVastTag = vastTag; + initPlayerView(); + } + } + public void setAccountId(final String accountId) { if (mAccountId == null) { mAccountId = accountId; @@ -92,7 +100,8 @@ private void initPlayerView() { addView(mPlayerView); boolean isFullscreenButtonHidden = mHideFullScreenButton != null ? mHideFullScreenButton : false; - mPlayerView.initVideo(mVideoId, mAccountId, mPolicyKey, mAutoplay, isFullscreenButtonHidden); + mPlayerView.initVideo(mVideoId, mAccountId, mPolicyKey, mAutoplay, isFullscreenButtonHidden, mVastTag); + // mPlayerView.initVideo(mVideoId, mAccountId, mPolicyKey, mAutoplay, isFullscreenButtonHidden); } } @@ -137,6 +146,7 @@ public void emitError(Event e) { } private boolean parametersSet() { - return mVideoId != null && mAccountId != null && mPolicyKey != null && mAutoplay != null && mHideFullScreenButton != null; + // return mVideoId != null && mAccountId != null && mPolicyKey != null && mAutoplay != null && mHideFullScreenButton != null; + return mVideoId != null && mAccountId != null && mPolicyKey != null && mAutoplay != null && mHideFullScreenButton != null && mVastTag != null; } }