Skip to content

Commit

Permalink
Enabled preroll ads on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Thiery committed Jan 5, 2018
1 parent 1fbd9bd commit b492893
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
11 changes: 6 additions & 5 deletions packages/brightcove-video/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.+"
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package uk.co.news.rntbrightcovevideo;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.util.Log;
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;
Expand All @@ -17,13 +22,19 @@
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();

private Boolean mAutoplay;
private Boolean mIsPlaying = false;
private Boolean mIsFullscreen = false;
private GoogleIMAComponent googleIMAComponent;
private float mProgress = 0;

public BrightcovePlayerView(final Context context) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<AdsRequest> adsRequests = new ArrayList<AdsRequest>(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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit b492893

Please sign in to comment.