New Cast extension (and demo app)

Santiago Seifert
AndroidX Media3
Published in
2 min readFeb 22, 2018

If your app already uses ExoPlayer, you will be happy to know that integrating Cast into your app has just become a bit easier, as it now implements the Player interface. Version 2.7 includes the new Cast extension along with a demo app to use as starting point.

ExoPlayer’s Cast demo app

Cast demo app launcher icon

The ExoPlayer+Cast integration demo app supports dynamic media playlist playback both locally and remotely using ExoPlayer and CastPlayer respectively. Playlist items can be added, reordered (by drag&dropping) and removed. In ExoPlayer, this is done using the recently introduced playlist features using ConcatenatingMediaSource.

Cast icon at the top-right corner of the screen

In order to try remote playback, a cast receiver device needs to be connected to the same network that the device running the demo app is connected to. If this is the case, the cast icon will appear at the top-right corner of the screen. Tapping on this icon will display a device selection dialog to pick the device with which the app is going to establish the cast session.

Using the Cast extension

The Cast extension provides a Player implementation using Cast’s RemoteMediaClient and related classes. Since CastPlayer implements Player, clients of ExoPlayer will require less work to depend on CastPlayer. The required steps for an app developer are:

  • Declare an OptionProvider class in the app’s AndroidManifest.xml. This will allow specifying the receiver app.
  • Initialize the cast context. Important note: Make sure you do this before your activity’s onResume(). Otherwise, your receiver devices might not be detected.
  • Start a CastSession. The Cast demo app does this by providing a MediaRouteButton.
  • Optional: Implement a custom receiver app. This is necessary for certain use cases, like DRM-protected media playback. The Cast demo app uses the default receiver app.
  • Optional: Implement player state transferring. The Cast Demo app does this to keep the queue and playback positions across playback locations.

Make sure you check out Google Cast’s official documentation, and the CastPlayer class. Along with useful documentation and media queue manipulation methods, it provides a convenient SessionAvailabilityListener which tracks the Cast Session state.

We are looking forward to receiving your feedback on the Cast extension, so please do not hesitate to contact us if you see any opportunity for improvement!

--

--