无法找到媒体路由,并且媒体路由actionBar项目或媒体路由按钮不可用

时间:2014-02-26 17:06:53

标签: android router google-cast

以下是我的代码。有一个可用的googlecast设备,但我找不到它。我已经逐步重建了简单的项目作为SDK文档,但仍然没有得到修复解决方案。所以重新打开这个问题。非常感谢你。

private MediaRouter mMediaRouter;
    private MediaRouteSelector mMediaRouteSelector;
    private MediaRouter.Callback mMediaRouterCallback;
    private CastDevice mSelectedDevice;
    private GoogleApiClient mApiClient;
    private Cast.Listener mCastListener;
    private ConnectionCallbacks mConnectionCallbacks;
   // private ConnectionFailedListener mConnectionFailedListener;
   // private HelloWorldChannel mHelloWorldChannel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
    mMediaRouteSelector = new MediaRouteSelector.Builder()
           // .addControlCategory(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)
                   /* .addControlCategory(CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID)
                    .addControlCategory(CastMediaControlIntent.EXTRA_CAST_APPLICATION_ID)
                    .addControlCategory(CastMediaControlIntent.EXTRA_CAST_RELAUNCH_APPLICATION)
                    .addControlCategory(CastMediaControlIntent.EXTRA_CAST_STOP_APPLICATION_WHEN_SESSION_ENDS)
                    .addControlCategory(CastMediaControlIntent.EXTRA_CUSTOM_DATA)
                    .addControlCategory(CastMediaControlIntent.EXTRA_DEBUG_LOGGING_ENABLED)
                    .addControlCategory(CastMediaControlIntent.EXTRA_ERROR_CODE)*/
            .addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
            .addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
            .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)

                    .build();
    mMediaRouterCallback = new MyMediaRouterCallback();
    //mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,MediaRouter.CALLBACK_FLAG_UNFILTERED_EVENTS);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    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(mMediaRouteSelector);
      return true;


}
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{

    return true;

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

// Remove the selector on stop to tell the media router that it no longer
// needs to discover routes for your app.
@Override
public void onStop() {
    mMediaRouter.removeCallback(mMediaRouterCallback);
    super.onStop();
}

1 个答案:

答案 0 :(得分:0)

见内联评论。再次感谢Leon Nicholls。