Elyes Mansour
Elyes Mansour

Reputation: 144

Chromecast keeps playing after clicking "stop casting" in default MediaRouteControllerDialog

Clicking on the "stop casting" button in the default dialog used by MediaRouteButton triggers the callbacks for SessionManagerListener::onSessionEnded and dismisses the Chromecast notification controls. However the video keeps playing on the TV.

I'm also using the com.google.android.exoplayer2.ext.cast.CastPlayer to start the Chromecast. The SessionAvailabilityListener::onCastSessionUnavailable callback is triggered as well.

This problem doesn't happen when using the close button of the Chromecast notification.

Upvotes: 0

Views: 570

Answers (1)

Elyes Mansour
Elyes Mansour

Reputation: 144

What happens when you click on the "stop casting" button is that this method MediaRouter::unselect is called. What you need to do is listen to when that happens and end the current session manually.

Here's an example:

MediaRouter.getInstance(context).addCallback(
    MediaRouteSelector
        .Builder()
        .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
        .build(),
    object: MediaRouter.Callback() {
        override fun onRouteUnselected(
            router: MediaRouter?,
            route: MediaRouter.RouteInfo?,
            reason: Int
        ) {
            if(reason in listOf(MediaRouter.UNSELECT_REASON_STOPPED, MediaRouter.UNSELECT_REASON_DISCONNECTED)) {
                CastContext.getSharedInstance(context).sessionManager.endCurrentSession(true)
            }
        }
    }
)

Upvotes: 1

Related Questions