androidchromecastgoogle-castandroid-cast-api

How to fully disconnect from Cast device when using RemoteDisplayLocalService?


I am using Cast SDK v2 and RemoteDisplayLocalService to cast local content. In the onRouteUnselected function in MediaRouter.Callback, I stop the service with CastRemoteDisplayLocalService.stopService(). The receiver app stops; however, the system pull-down still says the app is connected to the receiver, like so:

App is still connected

I assume this is because CastRemoteDisplayLocalService's GoogleApiClient is still connected to Google Play Services. Is there a programmatic way to completely disconnect the sender app?


Solution

  • Turns out this issue has nothing to do with the RemoteDisplay API. It was actually caused by my use of the MediaProjection API to cast the user's screen. The call

    mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data); 
    

    is what turns on the Cast icon in the pull down. To turn it off, call

    mMediaProjection.stop();
    

    Keep in mind that the user may stop the media projection at any time from the pulldown menu (which does not end the remote display session), so add the following callback to your MediaProjection:

    mMediaProjection.registerCallback(new MediaProjection.Callback() {
        @Override
        public void onStop() {
            // Stop casting when user stops media projection
            mMediaRouter.selectRoute(mMediaRouter.getDefaultRoute());
        }
    }, null);