Search code examples
androidchromecast

Control third-party Cast session from Android?


I want to observe and control a Chromecast session that was started by another app.

When connecting using MediaRouter, however, I cannot join an ongoing session without disrupting it. As soon as I select a route, my app replaces the previous one.

How can I monitor a Chromecast session using the Android SDK (and be able to play, pause, see progress, etc), without replacing the receiver app that is currently playing?


Solution

  • First Option:

    You can use a RemotePlaybackClient

    Controlling a remote playback route

    When you select a remote playback route your app acts as a remote control. The device at the other end of the route handles all content data retrieval, decoding, and playback functions. The controls in your app's UI communicate with the receiver device using a RemotePlaybackClient object.

    Javadoc:

    RemotePlaybackClient

    A helper class for playing media on remote routes using the remote playback protocol defined by MediaControlIntent.

    The client maintains session state and offers a simplified interface for issuing remote playback media control intents to a single route.

    You can create it with RemotePlaybackClient(Context context, MediaRouter.RouteInfo route)

    You can check if the MediaRouter.Route supports remote requests by calling route.supportsControlCategory

    Second Option:

    If your app does not support this you can try managing through the CastSession. This way you app will communicate with the other app through the chromeecast. With CastSession.getRemoteMediaClient() you get a RemoteMediaClient.