From 136f0876d515b5b04731ac85f497123f6c270557 Mon Sep 17 00:00:00 2001 From: jbibik Date: Tue, 8 Aug 2023 22:51:53 +0000 Subject: [PATCH] Fix `PlayerWrapper`'s creation of `VolumeProviderCompat` When hardware buttons are used to control the volume of the remote device, the call propagates to `MediaSessionCompat.setPlaybackToRemote(volumeProviderCompat)`. However, `volumeProviderCompat` was created incorrectly when the new device volume commands were present (COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS and COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS), i.e. with volumeControlType = `VOLUME_CONTROL_FIXED`. This resulted in `VolumeProviderCompat` which doesn't call `onSetVolumeTo` or `onAdjustVolume` and hence doesn't propagate the calls to the `Player`. Instead, it only worked with the deprecated commands which ensured the volumeControlType was `VOLUME_CONTROL_ABSOLUTE`. This bug was introduced in https://github.com/androidx/media/commit/c71e4bf1ffd4a0a0c631c27e4faa9480700e2c5f (1.0 media 3 release) when `PlayerWrapper`'s call to `createVolumeProviderCompat` was mostly rewritten to handle the new commands, but the two if-statements were not amended. Note: this change fixes the bug only for Android 11 and below. For 12 and above, there is a tracking bug for the regression that was introduced: https://issuetracker.google.com/issues/201546605 http://Issue: androidx/media#554 PiperOrigin-RevId: 554966361 (cherry picked from commit dedccc596eb75f9d3fcf7114e8541f3119a88382) --- RELEASENOTES.md | 6 ++++++ .../main/java/androidx/media3/session/PlayerWrapper.java | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 08cd87ff8f9..663e449cad0 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -43,6 +43,12 @@ `Result` that didn't support this which produced an `UnsuportedOperationException` ([#78](https://github.com/androidx/media/issues/78)). + * Fix the way `PlayerWrapper` creates a `VolumeProviderCompat` by + determining `volumeControlType` through both legacy commands + (`COMMAND_ADJUST_DEVICE_VOLUME` and `COMMAND_SET_DEVICE_VOLUME`) and new + commands (`COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS` and + `COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS`) + ([#554](https://github.com/androidx/media/issues/554)). * Test Utilities: * Add a `nanoTime()` method to `Clock` to provide override support of `System.nanoTime()` diff --git a/libraries/session/src/main/java/androidx/media3/session/PlayerWrapper.java b/libraries/session/src/main/java/androidx/media3/session/PlayerWrapper.java index 60aa78d6d7b..24c7cb019c6 100644 --- a/libraries/session/src/main/java/androidx/media3/session/PlayerWrapper.java +++ b/libraries/session/src/main/java/androidx/media3/session/PlayerWrapper.java @@ -1031,9 +1031,11 @@ public VolumeProviderCompat createVolumeProviderCompat() { } Commands availableCommands = getAvailableCommands(); int volumeControlType = VolumeProviderCompat.VOLUME_CONTROL_FIXED; - if (availableCommands.contains(COMMAND_ADJUST_DEVICE_VOLUME)) { + if (availableCommands.containsAny( + COMMAND_ADJUST_DEVICE_VOLUME, COMMAND_ADJUST_DEVICE_VOLUME_WITH_FLAGS)) { volumeControlType = VolumeProviderCompat.VOLUME_CONTROL_RELATIVE; - if (availableCommands.contains(COMMAND_SET_DEVICE_VOLUME)) { + if (availableCommands.containsAny( + COMMAND_SET_DEVICE_VOLUME, COMMAND_SET_DEVICE_VOLUME_WITH_FLAGS)) { volumeControlType = VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE; } }