How to launch the "Connect to device" dialog when using a custom cast button?

It seems that the official Google documentation on accessing Chromecast with Android revolves mainly around the ActionBar button, which seems to automatically handle the selection dialog and return the user's choice for the callback.

I have a user Buttonview with a method handleCastButton()in my activity. Is there an example somewhere how to call a standard selector when using the user interface?

Edit: It looks like I have to do something with a margin MediaRouteDialogFactory, but I cannot find any details.

+4
source share
3 answers

After analyzing the source code of MediaRouteButton, this works:

public void handleCastButton() {
    final FragmentManager fm = fragmentActivity.getSupportFragmentManager();
    MediaRouteChooserDialogFragment f = MediaRouteDialogFactory.getDefault().onCreateChooserDialogFragment();
    f.setRouteSelector(mediaRouteSelector);
    f.show(fm, "android.support.v7.mediarouter:MediaRouteChooserDialogFragment");
}

What is it! Make sure your base activity is FragmentActivityand all import data is from android.support.v7.*. The dialog fragment will be used Callbackassociated with the specified selector.

There is also MediaRouteControllerDialogFragmentif you are already connected and want to adjust the volume or mute.

+1
source

MediaRouteActionProvider, ActionBar, MediaRouteButton, ( , Chromecast ..), Button .

, ( png drawable-hdpi, drawable-mdpi drawable-xhdpi) .

+1

Take a look at this sample project , you can reuse it. You basically need to register to listen to the routes as they are discovered, as well as delete them and maintain a list of available ones and present them to the user when the user clicks on your button. When the user selects a route, you need to process the connection, and also indicate the route to the one selected in the media router instance.

-2
source

Source: https://habr.com/ru/post/1545448/


All Articles