Android Open Source - android-player-samples Main Activity






From Project

Back to project page android-player-samples.

License

The source code is released under:

Apache License

If you think the Android project android-player-samples listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.brightcove.player.samples.imawidevine.adrules;
// w w  w .  j a  v a2s .co m
import android.os.Bundle;
import android.util.Log;
import com.brightcove.drm.widevine.WidevinePlugin;
import com.brightcove.ima.GoogleIMAComponent;
import com.brightcove.ima.GoogleIMAEventType;
import com.brightcove.ima.GoogleIMAVideoAdPlayer;
import com.brightcove.player.event.Event;
import com.brightcove.player.event.EventEmitter;
import com.brightcove.player.event.EventListener;
import com.brightcove.player.event.EventType;
import com.brightcove.player.media.Catalog;
import com.brightcove.player.media.VideoFields;
import com.brightcove.player.media.VideoListener;
import com.brightcove.player.model.Video;
import com.brightcove.player.view.BrightcovePlayer;
import com.brightcove.player.view.BrightcoveVideoView;
import com.brightcove.player.util.StringUtil;
import com.google.ads.interactivemedia.v3.api.AdDisplayContainer;
import com.google.ads.interactivemedia.v3.api.AdsRequest;
import com.google.ads.interactivemedia.v3.api.ImaSdkFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This app illustrates how to use "Ad Rules" with the Google IMA
 * plugin, the Widevine plugin, and the Brightcove Player for Android.
 * Note: cue points are not used with Ad Rules.
 *
 * @author Paul Matthew Reilly (original code)
 * @author Paul Michael Reilly (added explanatory comments)
 */
public class MainActivity extends BrightcovePlayer {

    private final String TAG = this.getClass().getSimpleName();

    private EventEmitter eventEmitter;
    private GoogleIMAComponent googleIMAComponent;
    private String adRulesURL = "http://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=%2F15018773%2Feverything2&ciu_szs=300x250%2C468x60%2C728x90&impl=s&gdfp_req=1&env=vp&output=xml_vast2&unviewed_position_start=1&url=dummy&correlator=[timestamp]&cmsid=133&vid=10XWSh7W4so&ad_rule=1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // When extending the BrightcovePlayer, we must assign the BrightcoveVideoView before
        // entering the superclass. This allows for some stock video player lifecycle
        // management.
        setContentView(R.layout.activity_main);
        brightcoveVideoView = (BrightcoveVideoView) findViewById(R.id.brightcove_video_view);
        super.onCreate(savedInstanceState);
        eventEmitter = brightcoveVideoView.getEventEmitter();

        // Use a procedural abstraction to setup the Google IMA SDK via the plugin.
        setupGoogleIMA();

        // Initialize the widevine plugin.
        setupWidevine();

        // Create the catalog object which will start and play the video.
        Catalog catalog = new Catalog("FqicLlYykdimMML7pj65Gi8IHl8EVReWMJh6rLDcTjTMqdb5ay_xFA..");
        catalog.findVideoByID("2223563028001", new VideoListener() {
            @Override
            public void onVideo(Video video) {
                brightcoveVideoView.add(video);

                // Auto play: the GoogleIMAComponent will postpone
                // playback until the Ad Rules are loaded.
                brightcoveVideoView.start();
            }

            public void onError(String error) {
                Log.e(TAG, error);
            }
        });
    }

    /**
     * Setup the Brightcove IMA Plugin.
     */
    private void setupGoogleIMA() {
        // Establish the Google IMA SDK factory instance.
        final ImaSdkFactory sdkFactory = ImaSdkFactory.getInstance();

        // Enable logging up ad start.
        eventEmitter.on(GoogleIMAEventType.DID_START_AD, new EventListener() {
            @Override
            public void processEvent(Event event) {
                Log.v(TAG, event.getType());
            }
        });

        // 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.v(TAG, event.getType());
            }
        });

        // Enable Logging upon ad completion.
        eventEmitter.on(GoogleIMAEventType.DID_COMPLETE_AD, new EventListener() {
            @Override
            public void processEvent(Event event) {
                Log.v(TAG, event.getType());
            }
        });

        // 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) {
                // Create a container object for the ads to be presented.
                AdDisplayContainer container = sdkFactory.createAdDisplayContainer();
                container.setPlayer(googleIMAComponent.getVideoAdPlayer());
                container.setAdContainer(brightcoveVideoView);

                // 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(brightcoveVideoView, eventEmitter, true);

        // Calling GoogleIMAComponent.initializeAdsRequests() is no longer necessary.
    }

    private void setupWidevine() {
        // Set up the DRM licensing server to be handled by Brightcove with arbitrary device and
        // portal identifiers to fulfill the Widevine API contract.  These arguments will
        // suffice to create a Widevine plugin instance.
        String drmServerUri = "https://wvlic.brightcove.com/widevine/cypherpc/cgi-bin/GetEMMs.cgi";
        String deviceId = "device1234";
        String portalId = "brightcove";
        new WidevinePlugin(this, brightcoveVideoView, drmServerUri, deviceId, portalId);
    }
}




Java Source Code List

com.brightcove.player.samples.adobepass.webview.basic.MainActivity.java
com.brightcove.player.samples.adobepass.webview.basic.WebViewActivity.java
com.brightcove.player.samples.ais.webview.basic.ChooserResponse.java
com.brightcove.player.samples.ais.webview.basic.MainActivity.java
com.brightcove.player.samples.ais.webview.basic.ResourceAccessResponse.java
com.brightcove.player.samples.ais.webview.basic.WebViewActivity.java
com.brightcove.player.samples.captioning.dfxp.MainActivityTest.java
com.brightcove.player.samples.cast.basic.GoogleCastSampleFragment.java
com.brightcove.player.samples.cast.basic.MainActivity.java
com.brightcove.player.samples.freewheel.basic.MainActivity.java
com.brightcove.player.samples.freewheel.hls.MainActivity.java
com.brightcove.player.samples.freewheelwidevine.basic.MainActivity.java
com.brightcove.player.samples.hls.basic.MainActivity.java
com.brightcove.player.samples.hls.id3.MainActivity.java
com.brightcove.player.samples.ima.adrules.MainActivity.java
com.brightcove.player.samples.ima.basic.MainActivity.java
com.brightcove.player.samples.ima.hls.MainActivity.java
com.brightcove.player.samples.imawidevine.adrules.MainActivity.java
com.brightcove.player.samples.imawidevine.basic.MainActivity.java
com.brightcove.player.samples.omniture.basic.MainActivity.java
com.brightcove.player.samples.onceux.basic.MainActivity.java
com.brightcove.player.samples.texture.basic.MainActivity.java
com.brightcove.player.samples.webvtt.MainActivity.java
com.brightcove.player.samples.widevine.basic.MainActivity.java
com.brightcove.samples.android.bundledvideo.basic.MainActivity.java
com.brightcove.samples.android.closedcaptioning.dfxp.MainActivity.java