Enabling Google Cast support
Learn how to connect your Bitmovin Player to a cast-compatible device using Flutter.
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 supported platforms.
Have a look at our sample app showcasing Google Cast support in Flutter.
Setup
The following four steps are required to enable casting with the Bitmovin Player SDK in your Flutter 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
BitmovinCastManager
Before creating a Bitmovin Player instance that supports casting, the BitmovinCastManager
singleton has to be initialized once.
final castManager = await BitmovinCastManager.initialize();
Good to know
Optionally, an instance of
BitmovinCastManagerOptions
can be provided to theinitialize
function in order to configure the ID of the receiver application and a message namespace used for communication with the cast receiver.
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. The default value is true
.
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.flutter.example.MainActivity" />
</activity>
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="com.bitmovin.player.casting.BitmovinCastOptionsProvider" />
The activity used for the ExpandedControllerActivity has to be of type
FlutterFragmentActivity
rather thanFlutterActivity
.
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
}
Additional setup steps on iOS
On iOS, the Info.plist
file of the application has to be extended with local network and bluetooth permissions in order to allow finding and connecting to cast-compatible devices on your WiFi network.
<key>NSLocalNetworkUsageDescription</key>
<string>Required by Google Cast to use the local network to connect to Cast-enabled devices on your WiFi network</string>
<key>NSBonjourServices</key>
<array>
<string>_googlecast._tcp</string>
<string>_FFE417E5._googlecast._tcp</string>
</array>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Required by Google Cast</string>
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.
final source = Source(
sourceConfig: sourceConfig,
remoteControl: const SourceRemoteControlConfig(
castSourceConfig: SourceConfig(
url: 'https://bitmovin-a.akamaihd.net/content/art-of-motion_drm/mpds/11331.mpd',
type: SourceType.dash,
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.
Event | Description |
---|---|
CastAvailable | Emitted when casting to a cast-compatible device is available. |
CastStart | Emitted when casting is initiated, but the user still needs to choose which device should be used. |
CastWaitingForDevice | Emitted when a cast-compatible device has been chosen and the player is waiting for the device to get ready for playback. |
CastStarted | Emitted when the cast app is launched successfully. |
CastPlaying | Emitted when playback on a cast-compatible device has started. |
CastTimeUpdated | Emitted when the time update from the currently used cast-compatible device is received. |
CastPaused | Emitted when the playback on a cast-compatible device was paused. |
CastPlaybackFinished | Emitted when the playback on a cast-compatible device has finished. |
CastStopped | Emitted when casting to a cast-compatible device is stopped. |
Limitations
The cast feature currently has a few limitations when using it in Flutter.
Custom ExpandedControllerActivity
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 Android | Events additionally emitted on Android at the same time |
---|---|
CastPlaying | Playing |
CastPaused | Paused |
CastPlaybackFinished | PlaybackFinished |
Updated about 1 year ago