Chromecast custom MediaRouteChooserDialog

596 Views Asked by At

I am having trouble with customizing the MediaRouteChooser / MediaRouteController dialogs (I would need different layouts, some extra actions).

The cast button is set up by calling CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.media_route_menu_item); inside onCreateOptionsMenu.

I figured out that I would need to extend the MediaRouteDialogFactory where I return my own MediaRouteChooserDialogFragment where I overwrite the onCreateChooserDialog and return my custom dialog. However this needs to be a MediaRouteChooserDialog but it does not make much sense to extend this (you would need to duplicate all of the code, because everything is private and you can't use them e.g. mRouter, mCallback).

My question is that how can I do and show my own dialog and the route listing myself but also keep the CastButtonFactory.setUpMediaRouteButton solution inside the onCreateOptionsMenu? I really don't want to bother with the chromecast button states like visibility, connections state, decide if I need to show the route chooser or route controller etc. I am not even sure how should I do the route listing / managing discovery, so a little help would be nice also on that topic.

1

There are 1 best solutions below

1
On

I have solved this on my own, and it works great for me. Here is what I did in nutshell, maybe it gives some ideas.

I've created a CustomMediaRouteDialogFactory which extends the MediaRouteDialogFactory and provided my own dialogs by overriding onCreateChooserDialogFragment and onCreateControllerDialogFragment. These dialogs are extending the original chooser and controller dialogs from the framework.

The state of the cast button inside the toolbar is still managed automatically, I just passed my own route selector and dialog factory to the provider.

onCreateOptionsMenu:

MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
mediaRouteActionProvider.setRouteSelector(chromecastManager.getMediaRouteSelector());
mediaRouteActionProvider.setDialogFactory(mediaRouteDialogFactory);

Downside: I've ended up writing an own route discovery and filtering code by looking into the source code of the framework's chooser dialog implementation and some other dependencies. Secondly, the controller dialog is communicating with the RemoteMediaClient directly.

This way I could go full custom, but needed some extra work.