Search code examples
androidchromecast

Selecting MediaRoute on behalf of user works but MediaRoute button is in disconnected state


I'm building an app that will launch a chromecast receiver application automatically without the user clicking the mediaroute button and selecting a route. The user has already selected their desired route, and my app attempts to connect to it for a short period of time until it succeeds (hopefully).

When it does succeed, the app is launched onto the correct route, however the mediarouter menu item (the one used to select routes traditionally) is not updated, it still remains in the disconnected state. Once the user clicks on it (while the app is already connected to the receiver) it presents him with possible routes, and once he selects the route (the one that is already connected to and displaying the app) the media router menu item updates itself showing that it is connected and allowing the user to disconnect on the next click.

I would like it to be updated once successfully connected to the receiver (route). This way the user will be able to disconnect with only a couple clicks (once on the menu item, and once on the 'Stop Casting' button).

I am using the CastCompanionLibrary, though I do not think that it matters in this case.

I am able to launch the receiver without user action by calling the MediaRouter.Callback directly like so:

mCastManager.onRouteSelected(mMediaRouter, theRoute);

Where mMediaRouter is the MediaRouter instance and theRoute is the MediaRouter.RouteInfo instance holding the pre-selected receiver route.

Also, I am open to suggestions of better implementing automatic launch of the receiver application from a service/activity.


Solution

  • Call mMediaRouter.selectRoute(theRoute); when you have confirmed that you are connected and want to set the state of cast button to "connected".