Android:将Google Cast MediaRouter图标显示出来

时间:2014-10-12 19:45:48

标签: android chromecast

我尝试使用Chromecast测试一些内容,但遇到问题,让MediaRouter按钮显示在操作栏中。我已经使用cast dev控制台注册了我的设备,清单具有播放服务/互联网许可,并且我一直在关注文档。

MainActivity:

public class MainActivity extends ActionBarActivity {

private MediaRouteSelector mSelector;
private MediaRouter mMediaRouter;


private final MediaRouter.Callback mMediaRouterCallback =
        new MediaRouter.Callback() {

            @Override
            public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {

            }

            @Override
            public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route) {

            }

            @Override
            public void onRoutePresentationDisplayChanged(
                    MediaRouter router, MediaRouter.RouteInfo route) {

            }
        };


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMediaRouter = MediaRouter.getInstance( this );

    mSelector = new MediaRouteSelector.Builder()
            .addControlCategory(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)
            .build();
}

@Override
public void onStart() {
    mMediaRouter.addCallback(mSelector, mMediaRouterCallback,
            MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
    super.onStart();
}

@Override
public void onStop() {
    mMediaRouter.removeCallback(mMediaRouterCallback);
    super.onStop();
}


@Override
public boolean onCreateOptionsMenu(Menu 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(mSelector);

    return true;
}

}

菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>

<item android:id="@+id/media_route_menu_item"
    android:title="Chromecast"
    app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
    app:showAsAction="always"
    />
</menu>

Gradle依赖项:

compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.android.support:mediarouter-v7:19.0.+'
compile 'com.google.android.gms:play-services:6.1.11'

2 个答案:

答案 0 :(得分:1)

你快到了。尝试对选择器进行以下更改;将其更改为:

mSelector = new MediaRouteSelector.Builder().addControlCategory(CastMediaControlIntent.categoryForCast(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)).build();

看看是否有帮助。

答案 1 :(得分:0)

我知道您说您在开发控制台注册了,但是在检查更新时是否启用了转换设备发送其序列号?