Search code examples
androidcastingchromecastdisplay-manager

How to disconnect from Cast when finishing Activity?


Here is what I'm doing:

  • Manually connecting to ChromeCast via Settings.
  • Launching my app which manages Presentation to show custom layout using the ChromeCast device I'm already connected to.
  • Finishing my app by manually closing it.
  • By this time, I can still see my device screen casting. Here is where I want to disconnect from casting programmatically so I cannot longer see the screen casting.

While doing the presentation I have access to the Display I'm casting to:

MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
    return route != null ? route.getPresentationDisplay() : null;

Any ideas on how to achive this functionality?

UPDATE:

For those who are interested on this, this is how I did it (Thanks to Ali Naddaf response):

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void disconnect(){
    MediaRouter mMediaRouter = (MediaRouter) getSystemService(Context.MEDIA_ROUTER_SERVICE);
    mMediaRouter.selectRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, mMediaRouter.getDefaultRoute());
}

Solution

  • If I understand correctly, you are having user start a screen casting/mirroring outside of your app. Then user enters your app and when they leave, you want to disconnect? If I am not correct, please let me know.

    It is possible to disconnect the screen mirroring but I am not sure it is the right thing to do. When user enters your app, you do not know if the user had turned on the screen casting because of your app, or some other reason. If the user had done that for some other reason prior to entering your app, it wouldn't be right for your app to disconnect the user upon leaving. Would you agree?