Skip to content

Chromecast integration series

Integrating Red Bee OTT cast support

Before integrating the Enigma River Android cast-module please make sure you are familiar with the basics of implementing a chromecast sender application in Android. We recommend completing the 'Cast-enable an Android app' tutorial in Google Developers Code labs.


Tip: Before trying to cast using your app, make sure you can cast with a widely used app such as YouTube.

Prerequisites

This tutorial assumes that you have: * completed the basics series tutorial. * Working video app using Enigma River Android SDK and Red Bee OTT backend. * Google Chromecast media player device. * Android device with access to the same wifi as the chromecast.

Cast-module

The first thing needed in order to add cast functionality to your app is to add a dependency to the cast-module of the SDK.

// in build.gradle of your app-module
...
dependencies {
    ...
    implementation "com.github.EricssonBroadcastServices.EnigmaRiverAndroid:cast:__REPLACE_WITH_RELEASE_VERSION__"
    ...
}
...

The cast-module includes a transparent dependency to Google's Cast framework, so you do not need to explicitly add that dependency.

Setting up cast

OptionsProvider

To integrate cast into our app we need to provide an OptionsProvider for the Google Cast framework. This is done by adding a <meta-data>-tag in your AndroidManifest.xml:

<application ...>
    ...
    <meta-data
        android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
        android:value="com.redbeemedia.enigma.cast.optionsprovider.EnigmaCastOptionsProvider" />
    ...
 </application>

The Enigma River Android SDK provides this OptionsProvider out-of-the-box. If you need to customize any part of the CastOptions, EnigmaCastOptionsProvider is suitable for extension.

MediaRouter

When a connection to a chromecast receiver has been established the EnigmaCastManager will be notified. How the connection is managed is up to you, but we recommend using the standard button provided in the MediaRouter library.

// in build.gradle of your app-module
...
dependencies {
    ...
    implementation 'androidx.mediarouter:mediarouter:1.0.0'
    ...
}
...

Make sure your activities inherits from FragmentActivity or any of its descendants (for example AppCompatActivity). This is required for the MediaRouterButton to function. You may have to add a dependency to the appcompat library if you don't already have it:

// in build.gradle of your app-module
...
dependencies {
    ...
    implementation 'androidx.appcompat:appcompat:1.0.0'
    ...
}
...

Then add the MediaRouterButton to all of your activities. See Google's tutorial section on adding the cast button.

EnigmaCastManager

To initialize Google's Cast framework you need to call CastContext.getSharedInstance(applicationContext) or EnigmaCastManager.getSharedInstance(applicationContext) in onCreate of every activity or your app.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    ...
    EnigmaCastManager.getSharedInstance(getApplicationContext());
    ...
}

Your setup is now done.

Sending a PlayRequest to the chromecast receiver

To start playing an asset on the chromecast receiver, create an EnigmaCastRequest. You'll need to supply the assetID of the asset to be cast as well as a ISession object. This is done using a builder pattern:

IEnigmaCastRequest castRequest = new EnigmaCastRequest.Builder(assetId, session).build();
You can also supply the builder with an EnigmaCastPlaybackProperties object using .setPlaybackProperties(...).

To start casting simply call play(...) on the IEnigmaCastManager.

IEnigmaCastManager enigmaCastManager = EnigmaCastManager.getSharedInstance(getApplicationContext());

IEnigmaCastRequest castRequest = new EnigmaCastRequest.Builder(assetId, session).build();

enigmaCastManager.play(castRequest, new BaseEnigmaCastResultHandler() {
    @Override
    public void onSuccess() {
        //Success
    }

    @Override
    public void onException(Exception e) {
        //Failed
    }
});

Congratulations, you should now be able to cast Red Bee OTT media assets!

Advanced usage of the cast-module

Listening to events from the chromecast receiver

Custom events sent from the chromecast receiver app can be listened to by extending BaseEnigmaCastListener and adding the listener to the EnigmaCastManager.

...
IEnigmaCastManager enigmaCastManager = EnigmaCastManager.getSharedInstance(getApplicationContext());
enigmaCastManager.addCastListener(new BaseEnigmaCastListener() {
    @Override
    public void onVolumeChange(float volume, boolean muted) {
        //Volume changed
    }
    ...
    //Any other custom events of interest
    ...
});
...

By attaching the listener to the EnigmaCastManager the listener will receive all events for any currently active IEnigmaCastSession. The listener can also be attached to a specific IEnigmaCastSession and will in that case only receive events related to that cast session.

IEnigmaCastListener enigmaCastListener = new BaseEnigmaCastListener() {
    ...
    //events of interest
    ...
};

//Attach listener to current session if one exists
IEnigmaCastSession currentCastSession = enigmaCastManager.getCurrentEnigmaCastSession();
if(currentCastSession != null) {
    currentCastSession.addCastListener(enigmaCastListener);
}

//Listen for castSession changes
enigmaCastManager.addListener(new BaseEnigmaCastManagerListener() {
    @Override
    public void onCastSessionChanged(IEnigmaCastSession oldSession, IEnigmaCastSession newSession) {
        if(oldSession != null) {
            //Detach from old session
            oldSession.removeCastListener(enigmaCastListener);
        }
        if(newSession != null) {
            //Attach to new session
            newSession.addCastListener(enigmaCastListener);
        }
    }
});

Sending requests to the receiver app

To send a request to an active IEnigmaCastSession use the factory methods in EnigmaCastMessage to create a ICastControlRequest and send it using IEnigmaCastSession#sendMessage(...).

IEnigmaCastSession currentCastSession = enigmaCastManager.getCurrentEnigmaCastSession();

if(currentCastSession != null) {
    currentCastSession.sendMessage(EnigmaCastMessage.goToLive()); //For example
    //or currentCastSession.sendMessage(EnigmaCastMessage.playNextProgram());
    //or currentCastSession.sendMessage(EnigmaCastMessage.selectAudioTrack("de", null));
    //etc.
} else {
    //Currently not connected...
}