Skip to content

Commit

Permalink
Update available commands when MediaSessionCompat actions change
Browse files Browse the repository at this point in the history
This is a bug currently, where commands are created once but never
updated again if the actions in MediaSessionCompat are changed.

PiperOrigin-RevId: 525999084
  • Loading branch information
tonihei authored and rohitjoins committed Apr 24, 2023
1 parent 2de89ca commit 79fab67
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
* `void increaseDeviceVolume(int)`
* `void decreaseDeviceVolume(int)`
* `void setDeviceMuted(boolean, int)`
* Fix issue where `MediaController` doesn't update its available commands
when connected to a legacy `MediaSessionCompat` that updates its
actions.
* Audio:
* Fix bug where some playbacks fail when tunneling is enabled and
`AudioProcessors` are active, e.g. for gapless trimming
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1920,13 +1920,11 @@ private static ControllerInfo buildNewControllerInfo(
? newLegacyPlayerInfo.playbackInfoCompat.getVolumeControl()
: VolumeProviderCompat.VOLUME_CONTROL_FIXED;
availablePlayerCommands =
(oldControllerInfo.availablePlayerCommands == Commands.EMPTY)
? MediaUtils.convertToPlayerCommands(
newLegacyPlayerInfo.playbackStateCompat,
volumeControlType,
sessionFlags,
isSessionReady)
: oldControllerInfo.availablePlayerCommands;
MediaUtils.convertToPlayerCommands(
newLegacyPlayerInfo.playbackStateCompat,
volumeControlType,
sessionFlags,
isSessionReady);

PlaybackException playerError =
MediaUtils.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,36 @@ public void onPlayerErrorChanged(@Nullable PlaybackException error) {
assertThat(errorFromGetterRef.get().getMessage()).isEqualTo(testConvertedErrorMessage);
}

@Test
public void setPlaybackState_withActions_updatesAndNotifiesAvailableCommands() throws Exception {
MediaController controller = controllerTestRule.createController(session.getSessionToken());
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Player.Commands> commandsFromParamRef = new AtomicReference<>();
AtomicReference<Player.Commands> commandsFromGetterRef = new AtomicReference<>();
Player.Listener listener =
new Player.Listener() {
@Override
public void onAvailableCommandsChanged(Player.Commands commands) {
commandsFromParamRef.set(commands);
commandsFromGetterRef.set(controller.getAvailableCommands());
latch.countDown();
}
};
controller.addListener(listener);

session.setPlaybackState(
new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_FAST_FORWARD)
.build());

assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(commandsFromParamRef.get().contains(Player.COMMAND_PLAY_PAUSE)).isTrue();
assertThat(commandsFromParamRef.get().contains(Player.COMMAND_SEEK_FORWARD)).isTrue();
assertThat(commandsFromGetterRef.get().contains(Player.COMMAND_PLAY_PAUSE)).isTrue();
assertThat(commandsFromGetterRef.get().contains(Player.COMMAND_SEEK_FORWARD)).isTrue();
}

@Test
public void setPlaybackToRemote_notifiesDeviceInfoAndVolume() throws Exception {
int volumeControlType = VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE;
Expand Down

0 comments on commit 79fab67

Please sign in to comment.