Search code examples
google-cast

requestCastDeviceForRoute in onRouteAdded


I'm developing app for Chromecast. I'v tried Android Sample from https://github.com/googlecast/cast-android-sample, and he works fine - I may click on Cast icon, select device and start streaming. So, MediaRouter.Callback looks like this:

private class MyMediaRouterCallback extends MediaRouter.Callback {
    @Override
    public void onRouteSelected(MediaRouter router, RouteInfo route) {
        MediaRouteHelper.requestCastDeviceForRoute(route);
    }
}

And it works. But I want to start streaming on my device without stream button. So I'v changed this callback to:

private class MyMediaRouterCallback extends MediaRouter.Callback {
    @Override
    public void  onRouteAdded (MediaRouter router, RouteInfo route)
        MediaRouteHelper.requestCastDeviceForRoute(route);
    }
}

But this doesn't work. This callback fires, requestCastDeviceForRoute returns true with required device (I see it by route.getName()), but onDeviceAvailable from CastSampleActivity never called. I'v tried delayed call of requestCastDeviceForRoute using Handler.postDelayed, and still nothing.

So, how to use requestCastDeviceForRoute properly to stream on Chromecast device without Media Button, right after he will be detected in MediaRouter.Callback?


Solution

  • Try calling

    mediaRouter.selectRoute(route);
    

    instead of using the MediaRouterHelper. This has worked for me when calling from within onRouteAdded()