Can you add Chromecast functionality without using the ActionBar

对着背影说爱祢 提交于 2019-12-03 19:40:51

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();
  • Start the discovery by calling the following in your, say, onStart():

 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);

  • The above call results in another callback of your custom callback to be called, the onRouteSelected(). In that callback, call the following:

MediaRouteHelper.requestCastDeviceForRoute(info);

  • Finally, this will call the 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.

I found better solution:

    MediaRouteChooserDialog dialog =new MediaRouteChooserDialog(context);
    dialog.setRouteSelector(mediaRouteSelector);
    dialog.show();

Shows popup and will call onRouteSelected method on callback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!