How to disconnect from Cast when finishing Activity?
Asked Answered
B

4

5

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());
}
Booth answered 7/4, 2015 at 20:9 Comment(0)
R
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?

Rebhun answered 8/4, 2015 at 4:26 Comment(4)
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.Booth
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.Rebhun
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)Booth
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: github.com/googlecast/CastPresentation-androidRebhun
T
11

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

mSessionManager.endCurrentSession(true);
Tetrad answered 9/8, 2016 at 12:21 Comment(1)
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 :)Ground
S
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);
Stinger answered 14/2, 2018 at 9:57 Comment(0)
R
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?

Rebhun answered 8/4, 2015 at 4:26 Comment(4)
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.Booth
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.Rebhun
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)Booth
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: github.com/googlecast/CastPresentation-androidRebhun
A
0

This worked for me:

mediarouter.unselect(0)
Autocratic answered 3/5, 2016 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.