media icon indicating copy to clipboard operation
media copied to clipboard

Will getDeviceInfo() ever be supported for CastPlayer?

Open JaCobbMedia opened this issue 3 years ago • 1 comments

Describe your question in detail.

I'm developing a podcast app which supports chrome cast and we recently moved to media3. We are using MediaController to handle all playback and doing some UI changes regarding of what player is currently being used (exo/cast). For that we thought using onDeviceInfoChanged() in Player.Listener and doing changes according to playbackType would be a way to go, but it didn't seem to work so after some debugging I've noticed that CastPlayer doesn't support getDeviceInfo() method and always returns DeviceInfo.UNKNOWN with playbackType DeviceInfo.PLAYBACK_TYPE_LOCAL.

So my question is will getDeviceInfo() method ever be supported for CastPlayer and is there any other way to tell where playback is happening (exo/cast) from MediaController?

JaCobbMedia avatar Aug 04 '22 07:08 JaCobbMedia

I think it should be easy to provide a DeviceInfo with PLAYBACK_TYPE_REMOTE and min/maxVolume set to 0 (see CastPlayer.getVolume() that always returns 0 due to setting the volume not being supported with cast player). Marking as an enhancement.

If you are using ExoPlayer and CastPlayer only you should be able to do

controller.getDeviceInfo().equals(DeviceInfo.UNKNOWN)

If it delivers true, playback is happening on the cast player. It's a bit hacky but should work. After the change you should be able to check for PLAYBACK_TYPE_REMOTE as you suggest.

marcbaechinger avatar Aug 15 '22 16:08 marcbaechinger

@JaCobbMedia does onDeviceInfoChanged work for you out of the box? When is it triggered?

ziem avatar Jan 04 '23 12:01 ziem

On what event would you expect the onDeviceInfoChanged to be called?

In the context of the CastPlayer this issue is about, the device info only changes if MediaSession.setPlayer() is called and the player is changed for instance from ExoPlayer to CastPlayer (or vice versa). In this case the PlayerInfo is updated and sent to the controller. MediaControllerImpl would then receive the new PlayerInfo, find that its DeviceInfo is different and call onDeviceInfoChanged.

If this is not working for you, can you add some more details about how you change the DeviceInfo of the session?

marcbaechinger avatar Jan 04 '23 12:01 marcbaechinger

the device info only changes if MediaSession.setPlayer() is called and the player is changed for instance from ExoPlayer to CastPlayer (or vice versa).

@marcbaechinger thanks for the description you provided. Thanks to it, I think I know where my problem is. In my case MediaSession.setPlayer() is called once as I'm using a similar approach to the one described in https://github.com/androidx/media/issues/39. I implemented Player and the switch between ExoPlayer and CastPlayer happens internally without additional MediaSession.setPlayer() calls.

The composite player thing initialy seemsed like a good solution but do you see any other problems with it that I may encounter in the future?

ziem avatar Jan 05 '23 07:01 ziem

I think the DeviceInfo is a special case because it is kind of a static property of the player that actually never changes.

The composite approach makes IMO sense if a player is replaced by another player and this new player should continue the playback of the former player. Like when you want to transfer the playlist, mediaItemIndex and positionMs from the old to the new player.

For the special DeviceInfo case, you'd have to call the listeners from your composite ForwardingPlayer. Assumed you already have overridden addListener/removeListener, calling onDeviceInfoChange() should solve the issue. I can't think of a similar case where such a static property needs to be notified.

marcbaechinger avatar Jan 05 '23 12:01 marcbaechinger

@ziem I went with UAMP approach with ReplacableForwardingPlayer (because I needed a bit more control rather than just fully rely on MediaController) and basically call onDeviceInfoChange() whenever I switch players. But as @marcbaechinger said MediaController now calls onDeviceInfoChange() when session player gets changed and DeviceInfo doesn't match with previous, so if you're using MediaController it should work out of the box

JaCobbMedia avatar Jan 05 '23 13:01 JaCobbMedia

This helps a lot. Thank you both @JaCobbMedia & @marcbaechinger for your answers and solutions.

ziem avatar Jan 09 '23 08:01 ziem

Ok, cool. I close again as this is working as intended.

marcbaechinger avatar Jan 09 '23 23:01 marcbaechinger