4

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());
}
LarryMustaine
  • 65
  • 1
  • 6

4 Answers4

10

For the ones like me who came looking for the answer using v3 Cast SDK:

mSessionManager.endCurrentSession(true);
Alqueraf
  • 1,148
  • 1
  • 11
  • 26
  • It seems to me that all devices connected to the chromecast are disconnected, not only the one that requests endCurrentSession. Maybe there is some sweeter drop of connection :) – Manuela Jun 27 '17 at 16:12
5

Find SessionManager as below and disconnect from chromecast session:

    mRemoteMediaClient.stop(); // stop remote media
    CastContext castContext = CastContext.getSharedInstance(this);
    SessionManager mSessionManager = castContext.getSessionManager();
    mSessionManager.endCurrentSession(true);
Shivang
  • 935
  • 8
  • 18
0

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?

Ali Naddaf
  • 16,951
  • 2
  • 21
  • 28
  • Yes you are right, that's what I'm trying to achieve. I tried to create the cast connection using the MediaRouteActionProvider to show the cast icon at the ToolBar and connect from there, I used a default sender to configure the connection, but when trying to send the Presentation to the cast device it won't find any available display to do it, when even the cast was happening. I haven't found any example to create the connection and send the presentation to the cast device. From here is why I'm letting the user to manually connect and I wanted to disconnect when the activity is finished. – LarryMustaine Apr 08 '15 at 17:50
  • As of now, there is now API that allows developers create a screen-cast experience. If user is manually choosing to start the cast outside of your code, then instruct him/her to end it when they are leaving but don't try to disconnect programmatically for the reason I had stated; you do not know why the user, outside of your app, had started the screen cast in the first place. – Ali Naddaf Apr 08 '15 at 18:12
  • Ali, thank you for your support, I appreciate it so much. I'm totally agree with you about telling the user to disconnect instead of doing it by myself in code. So two things, First one, if there any example which instructs how to create a screen-cast experiencie to be ready to be use with the Presentation API, as I said, I have done the connection but wasn't able to detect an available display for the Presentation (for doing this I'm using the code that I posted on the question), and second one, how would you do to disconnect the screen-cast programmatically? (I agree on not doing this) – LarryMustaine Apr 08 '15 at 20:19
  • to disconnect, I imagine setting the route to Default Route should disconnect you; you can try that and see if it works or not: mMediaRouter.unselect(reason) or mMediaRouter.selectRoute(mMediaRouter.getDefaultRoute()). For a sample related to Presentation, checkout this: https://github.com/googlecast/CastPresentation-android – Ali Naddaf Apr 08 '15 at 20:30
0

This worked for me:

mediarouter.unselect(0)
Blapple
  • 67
  • 1
  • 1
  • 11