Enable Chromecast on the Sender on Android
This article explains how to enable the Chromecast Sender capabilities. This article also documents how you can connect with a custom Chromecast Receiver application.
Chromecast support is not available for the Android TV and Fire TV SDKs.
Prerequisites
- Your Sender device needs to be on the same Wi-Fi network as your Chromecast Receiver device.
Add the Chromecast integration
In order to enable Chromecast on the THEOplayer Android SDK, add the feature integration dependency in your build.gradle file.
implementation 'com.theoplayer.theoplayer-sdk-android:core:+'
implementation 'com.theoplayer.theoplayer-sdk-android:integration-cast:+' // add cast dependency
Set CastOptionsProvider
To enable Chromecast on Android a CastOptionsProvider should be specified.
This class contains the configuration for Chromecast on Android.
A DefaultCastOptionsProvider is provided as part of the THEOplayer Android SDK.
This DefaultCastOptionsProvider associates the default THEOplayer Chromecast receiver application with your Sender application.
You will need to register this class in your AndroidManifest.xml file within a meta-data tag under your application node,
as demonstrated at https://github.com/THEOplayer/samples-android-sdk/blob/master/Google-Cast/app/src/main/AndroidManifest.xml#L17-L19.
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="com.theoplayer.android.api.cast.chromecast.DefaultCastOptionsProvider"/>
Optional: Add routing button to your app
In order to get this button to appear in your application's Toolbar, the easiest way is to include it in the menu XML file for your Activity.
-
In your
resfolder add a menu folder -
Add an
activity_main_menu.xmlfile -
Add the following contents to this file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/media_route_menu_item"
android:title="Cast"
app:actionProviderClass="androidx.mediarouter.app.MediaRouteActionProvider"
app:showAsAction="always" />
</menu>
- Initialize this new
MenuItemin theonCreateOptionsMenumethod of your Activity. (Make sure you have anAppCompatActivity).
override fun onCreateOptionsMenu(menu: Menu): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.activity_main_menu, menu)
CastButtonFactory.setUpMediaRouteButton(applicationContext, menu, R.id.media_route_menu_item)
return true
}
Optional: Add MiniController to your app
In your main activity xml add the following fragment:
<fragment
android:id="@+id/castMiniController"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone"
class="com.google.android.gms.cast.framework.media.widget.MiniControllerFragment" />
Optional: Set a CastStrategy to your THEOplayerConfig
THEOplayer Android SDK support Chromecast CastStrategy to define the joining behavior.
THEOplayerConfig.Builder()
.castStrategy(CastStrategy.AUTO)
.build()
Connect with custom Chromecast Receiver application
The THEOplayer SDK on your Sender application connects with THEO Chromecast Receiver application by default,
but you can connect it to a custom Chromecast Receiver application instead. The default Dolby OptiView Chromecast Receiver application has OptiView Player functionality, but uses Shaka for media playback. If you want to use Google's legacy Media Player Library (MPL) for playback, you will need to use either a custom application or connect to Dolby OptiView's MPL Receiver application, using the appID value of 44BAE7D1. Note that the MPL receiver does not support HLS-CMAF streams.
To connect with a custom Chromecast Receiver application, you can create your own CastOptionsProvider by subclassing com.google.android.gms.cast.framework.OptionsProvider and implementing the getCastOptions method.
This approach is further demonstrated at https://github.com/THEOplayer/samples-android-sdk/tree/master/Google-Cast/guides/howto-google-cast-integration#initializing-cast-context,
and in the snippet below.
override fun getCastOptions(context: Context): CastOptions =
CastOptions.Builder()
.setReceiverApplicationId(DEFAULT_APP_ID)
.build()