Skip to content

Commit 777ef41

Browse files
committed
Drop stale events to fix race when device info changes
1 parent 1a2b650 commit 777ef41

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
@@ -139,13 +139,13 @@
139139
private final MediaSessionCompat sessionCompat;
140140
@Nullable private final MediaButtonReceiver runtimeBroadcastReceiver;
141141
@Nullable private final ComponentName broadcastReceiverComponentName;
142-
@Nullable private VolumeProviderCompat volumeProviderCompat;
143142
private final boolean playIfSuppressed;
144143
private final HandlerThread compatSessionInteractionThread;
145144
private final Handler compatSessionInteractionHandler;
146145

147146
private volatile long connectionTimeoutMs;
148147
@Nullable private FutureCallback<Bitmap> pendingBitmapLoadCallback;
148+
@Nullable private VolumeProviderCompat volumeProviderCompat;
149149
private int sessionFlags;
150150
@Nullable private LegacyError legacyError;
151151
private Bundle legacyExtras;
@@ -1674,6 +1674,10 @@ public void onAudioAttributesChanged(int seq, AudioAttributes audioAttributes) {
16741674
if (playbackType == DeviceInfo.PLAYBACK_TYPE_LOCAL) {
16751675
postOrRunForCompatSession(
16761676
() -> {
1677+
if (volumeProviderCompat != null) {
1678+
// Stale event.
1679+
return;
1680+
}
16771681
sessionCompat.setPlaybackToLocal(audioAttributes.getStreamType());
16781682
sessionImpl.onNotificationRefreshRequired();
16791683
});
@@ -1687,11 +1691,19 @@ public void onDeviceInfoChanged(int seq, DeviceInfo deviceInfo) {
16871691
if (volumeProviderCompat == null) {
16881692
int streamType = player.getAudioAttributesWithCommandCheck().getStreamType();
16891693
postOrRunForCompatSession(() -> {
1694+
if (volumeProviderCompat != null) {
1695+
// Stale event.
1696+
return;
1697+
}
16901698
sessionCompat.setPlaybackToLocal(streamType);
16911699
sessionImpl.onNotificationRefreshRequired();
16921700
});
16931701
} else {
16941702
postOrRunForCompatSession(() -> {
1703+
if (volumeProviderCompat == null) {
1704+
// Stale event.
1705+
return;
1706+
}
16951707
sessionCompat.setPlaybackToRemote(volumeProviderCompat);
16961708
sessionImpl.onNotificationRefreshRequired();
16971709
});

0 commit comments

Comments
 (0)