Skip to content

Commit 9da4d35

Browse files
committed
Drop stale events to fix race when device info changes
1 parent 6dd0c6d commit 9da4d35

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@
138138
private final MediaSessionCompat sessionCompat;
139139
@Nullable private final MediaButtonReceiver runtimeBroadcastReceiver;
140140
@Nullable private final ComponentName broadcastReceiverComponentName;
141-
@Nullable private VolumeProviderCompat volumeProviderCompat;
142141
private final boolean playIfSuppressed;
143142
private final HandlerThread compatSessionInteractionThread;
144143
private final Handler compatSessionInteractionHandler;
145144

146145
private volatile long connectionTimeoutMs;
147146
@Nullable private FutureCallback<Bitmap> pendingBitmapLoadCallback;
147+
@Nullable private VolumeProviderCompat volumeProviderCompat;
148148
private int sessionFlags;
149149
@Nullable private LegacyError legacyError;
150150
private Bundle legacyExtras;
@@ -1675,6 +1675,10 @@ public void onAudioAttributesChanged(int seq, AudioAttributes audioAttributes) {
16751675
if (playbackType == DeviceInfo.PLAYBACK_TYPE_LOCAL) {
16761676
postOrRunForCompatSession(
16771677
() -> {
1678+
if (volumeProviderCompat != null) {
1679+
// Stale event.
1680+
return;
1681+
}
16781682
sessionCompat.setPlaybackToLocal(audioAttributes.getStreamType());
16791683
sessionImpl.onNotificationRefreshRequired();
16801684
});
@@ -1688,11 +1692,19 @@ public void onDeviceInfoChanged(int seq, DeviceInfo deviceInfo) {
16881692
if (volumeProviderCompat == null) {
16891693
int streamType = player.getAudioAttributesWithCommandCheck().getStreamType();
16901694
postOrRunForCompatSession(() -> {
1695+
if (volumeProviderCompat != null) {
1696+
// Stale event.
1697+
return;
1698+
}
16911699
sessionCompat.setPlaybackToLocal(streamType);
16921700
sessionImpl.onNotificationRefreshRequired();
16931701
});
16941702
} else {
16951703
postOrRunForCompatSession(() -> {
1704+
if (volumeProviderCompat == null) {
1705+
// Stale event.
1706+
return;
1707+
}
16961708
sessionCompat.setPlaybackToRemote(volumeProviderCompat);
16971709
sessionImpl.onNotificationRefreshRequired();
16981710
});

0 commit comments

Comments
 (0)