Skip to content

Commit 79fab67

Browse files
toniheirohitjoins
authored andcommitted
Update available commands when MediaSessionCompat actions change
This is a bug currently, where commands are created once but never updated again if the actions in MediaSessionCompat are changed. PiperOrigin-RevId: 525999084
1 parent 2de89ca commit 79fab67

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

RELEASENOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
* `void increaseDeviceVolume(int)`
4444
* `void decreaseDeviceVolume(int)`
4545
* `void setDeviceMuted(boolean, int)`
46+
* Fix issue where `MediaController` doesn't update its available commands
47+
when connected to a legacy `MediaSessionCompat` that updates its
48+
actions.
4649
* Audio:
4750
* Fix bug where some playbacks fail when tunneling is enabled and
4851
`AudioProcessors` are active, e.g. for gapless trimming

libraries/session/src/main/java/androidx/media3/session/MediaControllerImplLegacy.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,13 +1920,11 @@ private static ControllerInfo buildNewControllerInfo(
19201920
? newLegacyPlayerInfo.playbackInfoCompat.getVolumeControl()
19211921
: VolumeProviderCompat.VOLUME_CONTROL_FIXED;
19221922
availablePlayerCommands =
1923-
(oldControllerInfo.availablePlayerCommands == Commands.EMPTY)
1924-
? MediaUtils.convertToPlayerCommands(
1925-
newLegacyPlayerInfo.playbackStateCompat,
1926-
volumeControlType,
1927-
sessionFlags,
1928-
isSessionReady)
1929-
: oldControllerInfo.availablePlayerCommands;
1923+
MediaUtils.convertToPlayerCommands(
1924+
newLegacyPlayerInfo.playbackStateCompat,
1925+
volumeControlType,
1926+
sessionFlags,
1927+
isSessionReady);
19301928

19311929
PlaybackException playerError =
19321930
MediaUtils.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat);

libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaControllerWithMediaSessionCompatTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,36 @@ public void onPlayerErrorChanged(@Nullable PlaybackException error) {
15131513
assertThat(errorFromGetterRef.get().getMessage()).isEqualTo(testConvertedErrorMessage);
15141514
}
15151515

1516+
@Test
1517+
public void setPlaybackState_withActions_updatesAndNotifiesAvailableCommands() throws Exception {
1518+
MediaController controller = controllerTestRule.createController(session.getSessionToken());
1519+
CountDownLatch latch = new CountDownLatch(1);
1520+
AtomicReference<Player.Commands> commandsFromParamRef = new AtomicReference<>();
1521+
AtomicReference<Player.Commands> commandsFromGetterRef = new AtomicReference<>();
1522+
Player.Listener listener =
1523+
new Player.Listener() {
1524+
@Override
1525+
public void onAvailableCommandsChanged(Player.Commands commands) {
1526+
commandsFromParamRef.set(commands);
1527+
commandsFromGetterRef.set(controller.getAvailableCommands());
1528+
latch.countDown();
1529+
}
1530+
};
1531+
controller.addListener(listener);
1532+
1533+
session.setPlaybackState(
1534+
new PlaybackStateCompat.Builder()
1535+
.setActions(
1536+
PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_FAST_FORWARD)
1537+
.build());
1538+
1539+
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
1540+
assertThat(commandsFromParamRef.get().contains(Player.COMMAND_PLAY_PAUSE)).isTrue();
1541+
assertThat(commandsFromParamRef.get().contains(Player.COMMAND_SEEK_FORWARD)).isTrue();
1542+
assertThat(commandsFromGetterRef.get().contains(Player.COMMAND_PLAY_PAUSE)).isTrue();
1543+
assertThat(commandsFromGetterRef.get().contains(Player.COMMAND_SEEK_FORWARD)).isTrue();
1544+
}
1545+
15161546
@Test
15171547
public void setPlaybackToRemote_notifiesDeviceInfoAndVolume() throws Exception {
15181548
int volumeControlType = VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE;

0 commit comments

Comments
 (0)