当我们选择没有CastButton的MediaRouter.RouteInfo时,mySessionManager.getCurrentCastSession()返回null

时间:2018-01-16 14:49:24

标签: java android chromecast

我们想要在没有CastButton的情况下打开选择MediaRouter.RouteInfo的CastSession。 为此,我们使用了下面的代码,我们选择了一个名为Living room的MediaRouter.RouteInfo。但是,方法onRouteSelected mySessionManager.getCurrentCastSession()重新调整null

如何在没有CastSession的情况下打开CastButton

我们怎么做?This is the API reference

    private class MyMediaRouterCallback extends MediaRouter.Callback {
    @Override
    public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
        // Add route to list of discovered routes
        synchronized (this) {
            if(route.getDeviceType() != MediaRouter.RouteInfo.DEVICE_TYPE_UNKNOWN) {
                mRouteInfos.add(route);
                mRouteNames.add(route.getName() + " (" + route.getDescription() + ")");
                Log.v(TAG, "Device Type: " + route.getDeviceType());
                Log.v(TAG, "Custom Route Name: " + mRouteNames.get(mRouteCount));


                if(route.getName().equals("Living room")) {
                    Log.v(TAG, "Selecciono route "+ route.getName());
                    mMediaRouter.selectRoute(route);
                }
            }
        }
    }

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

        // Remove route from list of routes
        synchronized (this) {
            for (int i = 0; i < mRouteInfos.size(); i++) {
                MediaRouter.RouteInfo routeInfo = mRouteInfos.get(i);
                if (routeInfo.equals(route)) {
                    mRouteInfos.remove(i);
                    mRouteNames.remove(i);
                    return;
                }
            }
        }
    }

    @Override
    public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) {
        // Handle route selection.
        mSelectedDevice = CastDevice.getFromBundle(info.getExtras());

        mCastSession = mySessionManager.getCurrentCastSession();

        if(mCastSession!=null)
            Log.v(TAG, "2 mCastSession isConnected: "+    mCastSession.isConnected());
       else
            Log.v(TAG, "2 myCastSession is null: ");


        // Just display a message for now; In a real app this would be the
        // hook to connect to the selected device and launch the receiver
        // app
    }

    @Override
    public void onRouteUnselected(MediaRouter router, MediaRouter.RouteInfo info) {
        Log.d(TAG, "onRouteUnselected: info=" + info);
        mSelectedDevice = null;
    }
}

0 个答案:

没有答案