7

支持 Chromecast 的 Netflix 和 YouTube 应用程序在操作栏中显示 MediaRouteButton,但Sender 应用程序教程仅说明如何在布局中显示它。如何在操作栏中显示它?

4

1 回答 1

17

您可以通过将其添加到操作栏菜单来将其显示在操作栏中:

<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;
}
于 2013-07-28T19:05:45.620 回答