Search code examples
androidchromecastgoogle-cast

What is the correct callback to get mute events from the Chromecast Receiver?


I'm running into an issue where, when two devices are connected to the same cast session and when BaseCastManager.setDeviceMute() is called from device 1, device 2 does not receive a MediaCallback.onRouteVolumeChanged()

What's the proper way to do this? Is there perhaps a different callback?

Here's what eventually gets called.

/**
 * Mutes or un-mutes the device volume.
 *
 * @throws CastException
 * @throws NoConnectionException
 * @throws TransientNetworkDisconnectionException
 */
public final void setDeviceMute(boolean mute) throws CastException,
        TransientNetworkDisconnectionException, NoConnectionException {
    checkConnectivity();
    try {
        Cast.CastApi.setMute(mApiClient, mute);
    } catch (IOException e) {
        throw new CastException("setDeviceMute", e);
    } catch (IllegalStateException e) {
        throw new NoConnectionException("setDeviceMute()", e);
    }
}

Here's my listener:

private class MediaCallBack extends android.support.v7.media.MediaRouter.Callback {
        @Override
        public void onRouteVolumeChanged(MediaRouter router,
                                         RouteInfo route) {
            super.onRouteVolumeChanged(router, route);
            onVolumeChanged(route.getVolume());
        }


    }

Solution

  • Since you are using Cast Companion Library, you can use the callback VideoCastConsumer#onVolumeChanged(double value, boolean isMute) by extending VideoCastConsumerImpl, overriding that method and registering it with the VideoCastManager(and unregister it when not needed anymore):

    VideoCastConsumer myConsumer = new VideoCastConsumerImpl() {
       void onVolumeChanged(double value, boolean isMute) {
           // do as you wish here
       }
    }
    
    VideoCastManager.getInstance().addVideoCastConsumer(myConsumer);