4

It seems that Google official documentation on accessing a Chromecast from Android revolves mostly around an ActionBar button that seems to automatically handle the chooser dialog and return the user choice to the Callback.

I have a custom Button (view) with a handleCastButton() method in my activity. Is there an example somewhere of how to bring up the standard chooser when using a custom UI?

Edit: It looks like I should be able to do something with the stock MediaRouteDialogFactory but I can't find any details.

Brian White
  • 8,332
  • 2
  • 43
  • 67

3 Answers3

1

If you are not using the MediaRouteActionProvider to add the cast button to the ActionBar, you should instead use a MediaRouteButton which has the same behavior (bringing up the standard selector dialog, automatically changing state based on whether there are Chromecasts available, etc) but can be placed anywhere a normal Button can be.

Custom styling can be done by copying the default images drawables (and the associated pngs in drawable-hdpi, drawable-mdpi, and drawable-xhdpi) and styling them or just provide overriding resources of the same names.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I tried the MediaRouteButton but the icon does not match well with the other icons of my app and I'll need to be able to have more control over what happens when the button is pressed. Actually, if there's a way to just change the icon of the button, I could probably handle the rest. – Brian White Jun 21 '14 at 15:07
  • You can style the MediaRouteButton as per [this post](https://plus.google.com/102302322819366398844/posts/Z7BmFWVf61j). Added details on styling to my answer – ianhanniballake Jun 21 '14 at 15:49
  • Thanks for the info. It looks like I can also create my own `MediaRouteDialogFactory` and call `setDialogFactory()` with it if I want to change some or all of the dialogs shown when pressing buttons. – Brian White Jun 22 '14 at 04:00
1

After analyzing the MediaRouteButton source code, this seems to work:

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

That's it! Make sure your base Activity is a FragmentActivity and all imports are from android.support.v7.*. The dialog fragment will use the Callback you've associated with said selector.

There is also a MediaRouteControllerDialogFragment for when you've already connected and want to adjust the volume or disconnect.

Brian White
  • 8,332
  • 2
  • 43
  • 67
  • This works for connecting but when already connected to a cast device, the chooser does not give the option for disconnecting from the device. It shows the connected device and indicates it correctly as casting but pressing it does not do anything. I'd expect the onRouteUnselected method of the MediaRouter.Callback to be called, but it's not. Any idea how to achieve that behavior? – Micky Nov 07 '14 at 11:30
  • Ah, found it in the MediaRouteButton source, too: 'MediaRouteControllerDialogFragment f = MediaRouteDialogFactory.getDefault().onCreateControllerDialogFragment(); f.show(fm, "android.support.v7.mediarouter:MediaRouteControllerDialogFragment");' – Micky Nov 07 '14 at 11:39
  • Is not working for me :( just spinning and "searching for devices..." My phone is able to do screen mirroring via built in Android functionality. – kellogs Aug 21 '15 at 12:02
  • If you're seeing a dialog, then you've gotten _this_ part to work. If it can't find devices, then you probably have a configuration problem elsewhere. – Brian White Aug 24 '15 at 03:06
  • Wait a minute.Seems like this way is not the right way for Miracast dongle discovery, as Mark Murphy tells us in the comments here - http://stackoverflow.com/questions/29524643/how-to-get-mediarouteselector-to-show-available-miracast-and-chromecast-devices. Have you used this method for Miracast successfully ? – kellogs Sep 01 '15 at 21:29
  • @kellogs, I have only used it for a Chromecast but it shouldn't matter. The choice between types of devices is done in the setup of the _media route selector_ (not shown above) and not about getting the button to display a dialog which is the subject of this question. Indeed, the SO question you link is all about the `MediaRouteSelector`. – Brian White Sep 06 '15 at 02:22
-2

Take a look at this sample project, you should be able to reuse some of it. You basically need to register to listen for routes as they are found and as they are removed and maintain a list of available ones and present that to user whenever user clicks on your button. When user selects a route, you need to handle connection and also set the route to selected one in the media router instance.

Ali Naddaf
  • 16,951
  • 2
  • 21
  • 28
  • I have tried that sample project. When I run it, I get four buttons on the screen all of which open completely blank activities (on my N7). Also, the app source appears to create its own selection dialog rather than cause the stock one to be opened. – Brian White Jun 21 '14 at 15:10
  • If you are going to put your own button (i.e., not using the ActionProvider, nor using MediaRouteButton), then you are on your own to create the dialog; you don't have direct access to the stock dialog for media route chooser dialog. – Ali Naddaf Jun 21 '14 at 16:48