Adding Dynamic Cast Button to Android Application

1.6k Views Asked by At

From the Google documentation about adding a Cast button, https://developers.google.com/cast/docs/android_sender#adding-the-cast-button , it seems like the option they provide is for us to add a static button to the Action Bar. That said, what would I do if I wanted to add a Cast Button to my application only if there's a device on the network dynamically? Also, what are some ways you would create a custom button that mimic the functionality of a MediaRouter Button.

Any input would be appreciated!

2

There are 2 best solutions below

1
On

Based on your response in the comments, it seems like you want the normal behavior of a cast button, provided by, say, the MediaRouteActionProvider except that you don't want the cast discovery to include "guest mode". Is that a fair rephrasing of your question? If that is the case, then you can simply use the MediaRouteActionProvider and when you add your app in the developer console, you can uncheck the box that says "Supports Google Cast Guest Mode". Then guest mode (i.e. discovery of your app when not on wifi) is disabled for app and only devices that are on the same wifi network as your phone will be discoverable for your app.

1
On

After following the steps for incorporating cast in your application. Eg https://github.com/googlecast/CastHelloText-android/blob/master/src/com/example/casthelloworld/MainActivity.java Create an android button and call the following method in the button

    public void connect() {
    android.media.MediaRouter mMediaRouter = (android.media.MediaRouter)getApplicationContext().getSystemService(Context.MEDIA_ROUTER_SERVICE);
    mMediaRouter.selectRoute(android.media.MediaRouter.ROUTE_TYPE_USER,mMediaRouter.getRouteAt(1));
}

This automatically connects to the chromecast and calls the @Override public void onRouteSelected(MediaRouter router, RouteInfo info) method.