解决方法
您可以将其添加到操作栏菜单中,在操作栏中显示它:
<item android:id="@+id/action_mediaroute" android:actionProviderClass="android.support.v7.app.MediaRouteActionProvider" android:actionViewClass="android.support.v7.app.MediaRouteButton" android:showAsAction="always" android:title="@string/action_chromecast"/>
然后在onCreateOptionsMenu中提供初始化:
public boolean onCreateOptionsMenu( Menu menu ) { getMenuInflater().inflate( R.menu.main,menu ); MenuItem mediaRouteItem = menu.findItem( R.id.action_mediaroute ); mMediaRouteButton = (MediaRouteButton) mediaRouteItem.getActionView(); mCastContext = new CastContext( getApplicationContext() ); MediaRouteHelper.registerMinimalMediaRouteProvider( mCastContext,this ); mMediaRouter = MediaRouter.getInstance( getApplicationContext() ); mMediaRouteSelector = MediaRouteHelper.buildMediaRouteSelector( MediaRouteHelper.CATEGORY_CAST ); mMediaRouteButton.setRouteSelector( mMediaRouteSelector ); mMediaRouterCallback = new MyMediaRouterCallback(); return true; }