单击媒体路由按钮不会显示我想要的内容

时间:2017-01-20 13:59:47

标签: android miracast

我想在我的Android应用程序中添加miracast技术。

我想要mediaRouteButton,它显示了点击后连接它们的可能显示列表。怎么做到这个? 我尝试了这段代码(操作栏中的路由按钮):

public class MainActivity extends AppCompatActivity {

private Context context;

private MediaRouter.RouteInfo mRoute;
private MediaRouter mMediaRouter;
private MediaRouteSelector mSelector;
private RemotePlaybackClient mRemotePlaybackClient;

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

    mMediaRouter = MediaRouter.getInstance(this);

    mSelector = new MediaRouteSelector.Builder()
            .addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
            .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
            .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();
}

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

            @Override
            public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {
                Log.d("TAG", "onRouteSelected: route=" + route);

                if (route.supportsControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)){

                    mRoute = route;
                    mRemotePlaybackClient = new RemotePlaybackClient(context, mRoute);
                }
            }

            @Override
            public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo route, int reason) {
                Log.d("TAG", "onRouteUnselected: route=" + route);

                if (route.supportsControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)){

                    if (mRoute != null && mRemotePlaybackClient != null) {
                        mRemotePlaybackClient.release();
                        mRemotePlaybackClient = null;
                    }

                    mRoute = route;

                    if (reason != MediaRouter.UNSELECT_REASON_ROUTE_CHANGED) {

                    }
                }
            }
};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu, menu);

    MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
    MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider) MenuItemCompat.getActionProvider(mediaRouteMenuItem);

    mediaRouteActionProvider.setRouteSelector(mSelector);

    return true;
}


}

并在我的menu.xml中:

<?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="menu item"
          app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
          app:showAsAction="always"/>

</menu>

但是,当我点击这个菜单按钮时,它会显示4个奇怪项目的对话框,而不是我可以连接的显示器。点击该项后,应用程序崩溃了。 那么,我该如何实现这个功能呢?按钮,然后单击此按钮,显示可能的显示列表。谢谢。

0 个答案:

没有答案