I've been working from the example applications from Google to prepare our Chromecast capabilities,
What I've found however is that you are required to have a button in the ActionBar
(as implemented by Google) in order to get an ActionProvider
in order to attach your selector to.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);
mediaRouteActionProvider.setRouteSelector(mCaster.getMediaRouteSelector());
return true;
}
Intact, the entire example and API seems to be hot-wired to centre around the use of this button. I'm fine with reproducing the look and feel the Android are trying to go with here - but I use a custom ActionBar in my app which means I don't have any "hooks" to select the route et al.
Does anyone know how to work around this?
To begin with, I suggest you consider moving to appcompat actionbar so you can get all the benefits moving forward out of the box.
That said, the answer to your question is: yes, it is doable if you are willing to do a bit of extra work to manage the lifecycle and states yourself. I outline the steps here so you can create a working example for yourself. The requirements is to have appcompat and mediarouter from v7 support library in your project but you don't need to use any specific type of actionbar or no need to use the MediaRouteButton; in fact you can trigger the discovery and the rest in any shape or form that you want. Here are the steps:
Make sure appcompat and mediarouter from v7 support library are included in your dependencies. It is important to have v7 version of mediarouter
Do the usual thing, i.e. build a CastContext
and set the stage ready for discovery (you need to create an instance of MediaRouteAdapter
, which I have called myMediaRouteAdapter
):
mCastContext = new CastContext(getApplicationContext());
MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext,
myMediaRouteAdapter);
mMediaRouter = MediaRouter.getInstance(getApplicationContext());
mMediaRouteSelector = MediaRouteHelper
.buildMediaRouteSelector(MediaRouteHelper.CATEGORY_CAST,
"YOUR_APP_NAME", null);
mMediaRouterCallback = new MyMediaRouterCallback();
mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
In your custom callback MyMediaRouterCallback
, listen for routes as they get added or removed. As routes get discovered, your onRouteAdded()
method of your callback will be called and as they go away, the onRouteRemoved()
will be called. It will be now your responsibility to hold on to the list of valid routes in your choice of data structure.
Lets say, for the sake of argument, you present the list to users in a dialog (makes sense, doesn't it?) where user sees a the name of route (i.e. device) in the UI and each item represents a RouteInfo
. Then when a user clicks on a route, you need to call
mMediaRouter.selectRoute(info);
onRouteSelected()
. In that callback, call the following:
MediaRouteHelper.requestCastDeviceForRoute(info);
onDeviceAvailable()
callback of the myMediaRouteAdapter
and passes a CastDevice
to you which you should be able to grab and use. When you are done with a route and want to "deselect" it, call
mMediaRouter.selectRoute(mMediaRouter.getDefaultRoute());
Hopefully that is enough to get you going.