Enabling Google Cast support

Learn how to connect your Bitmovin Player to a cast-compatible device using React Native.

Google Cast support allows a user to play a source on on a cast-compatible device, while controlling playback using the Player.

Google Cast support is available on all platforms except tvOS and Android TV.

📘

Have a look at our sample app showcasing Google Cast support in React Native.

Setup

The following four steps are required to enable casting with the Bitmovin Player SDK in your React Native app.

Adding the dependencies

The following dependencies have to be added manually to Android's build.gradle as well as iOS's Podfile.

You can get the correct version of the cast dependencies by referring to the sample app.

Android

implementation("com.google.android.gms:play-services-cast-framework:21.3.0")
implementation("androidx.mediarouter:mediarouter:1.3.1")

iOS

pod 'google-cast-sdk', '4.8.0'

Initializing the BitmovinCastManager

Before creating a Bitmovin Player instance that supports casting, the BitmovinCastManager singleton has to be initialized once.

BitmovinCastManager.initialize();

👍

Good to know

Optionally, an instance of BitmovinCastManagerOptions can be provided to the initialize function in order to configure the id of the receiver application and a message namespace used for communication with the cast receiver.

On Android it is also necessary to update the context. When using multiple Activities, this has to be done in every activity.

if (Platform.OS === 'android') {
  BitmovinCastManager.updateContext();
}

Configuring the Player

The RemoteControlConfig is used to configure the remote playback behaviour of the Player instance. Most prominently, RemoteControlConfig.isCastEnabled defines whether casting is enabled in a certain Player instance or not.

Additional setup steps on Android

The cast feature requires some additional setup on Android. In the Android manifest, the ExpandedControllerActivity as well as the CastOptionsProvider have to be declared:

<activity
        android:name="com.bitmovin.player.casting.ExpandedControllerActivity"
        android:exported="true"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.bitmovin.player.reactnative.example.MainActivity" />
</activity>

<meta-data
      android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
      android:value="com.bitmovin.player.casting.BitmovinCastOptionsProvider" />

In the native Activity, the following code has to be added to the onCreate function to ensure that the cast state is updated correctly:

    try {
      CastContext.getSharedInstance(this, Runnable::run);
    } catch (Exception e) {
      // cast framework not supported
    }

Switching sources when Casting on iOS

A common use case is to play HLS streams with FairPlay DRM protection on iOS devices. This solution works very well for protecting the content whilst ensuring compatibility with the iOS native system.

However, this presents some issues if you'd like to enable casting to devices that do not support FairPlay. In scenarios like this, it's possible to instruct the player to switch to a different streaming and DRM format that is compatible, such as DASH + Widevine DRM.

source.remoteControl = {
  castSourceConfig: {
    url: 'https://bitmovin-a.akamaihd.net/content/art-of-motion_drm/mpds/11331.mpd',
    type: SourceType.DASH,
    title: 'Art of Motion',
    drmConfig: {
      widevine: {
        licenseUrl: 'https://cwip-shaka-proxy.appspot.com/no_auth',
      },
    },
  },
};

A full example of this can be found here.

Supported Events

The following events are emitted while casting to a remote-enabled device.

EventDescription
CastAvailableEmitted when casting to a cast-compatible device is available.
CastStartEmitted when casting is initiated, but the user still needs to choose which device should be used.
CastWaitingForDeviceEmitted when a cast-compatible device has been chosen and the player is waiting for the device to get ready for playback.
CastStartedEmitted when the cast app is launched successfully.
CastPlayingEmitted when playback on a cast-compatible device has started.
CastTimeUpdatedEmitted when the time update from the currently used cast-compatible device is received.
CastPausedEmitted when the playback on a cast-compatible device was paused.
CastPlaybackFinishedEmitted when the playback on a cast-compatible device has finished.
CastStoppedEmitted when casting to a cast-compatible device is stopped.

Limitations

The cast feature currently has a few limitations when using it in React Native.

Custom ExpandedControllerActivity

It is currently not possible to define and use a custom ExpandedControllerActivity on Android.

Different Events on Android and iOS

In the future, normal playback related events should be emitted during cast playback instead of the cast specific ones to simplify integration. On Android, the non cast specific events are already emitted at the same time as the cast specific ones.

Events emitted on iOS and AndroidEvents additionally emitted on Android at the same time
CastPlayingPlaying
CastPausedPaused
CastPlaybackFinishedPlaybackFinished