mirror of
https://github.com/androidx/media.git
synced 2025-05-12 18:19:50 +08:00
Use onEvents in MediaSessionConnector to avoid multiple updates.
#exofixit PiperOrigin-RevId: 344065556
This commit is contained in:
parent
c17d1091f3
commit
58e01671e1
@ -15,6 +15,13 @@
|
|||||||
*/
|
*/
|
||||||
package com.google.android.exoplayer2.ext.mediasession;
|
package com.google.android.exoplayer2.ext.mediasession;
|
||||||
|
|
||||||
|
import static com.google.android.exoplayer2.Player.EVENT_IS_PLAYING_CHANGED;
|
||||||
|
import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_PARAMETERS_CHANGED;
|
||||||
|
import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED;
|
||||||
|
import static com.google.android.exoplayer2.Player.EVENT_PLAY_WHEN_READY_CHANGED;
|
||||||
|
import static com.google.android.exoplayer2.Player.EVENT_REPEAT_MODE_CHANGED;
|
||||||
|
import static com.google.android.exoplayer2.Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -38,7 +45,6 @@ import com.google.android.exoplayer2.ControlDispatcher;
|
|||||||
import com.google.android.exoplayer2.DefaultControlDispatcher;
|
import com.google.android.exoplayer2.DefaultControlDispatcher;
|
||||||
import com.google.android.exoplayer2.ExoPlaybackException;
|
import com.google.android.exoplayer2.ExoPlaybackException;
|
||||||
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
|
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
|
||||||
import com.google.android.exoplayer2.PlaybackParameters;
|
|
||||||
import com.google.android.exoplayer2.Player;
|
import com.google.android.exoplayer2.Player;
|
||||||
import com.google.android.exoplayer2.Timeline;
|
import com.google.android.exoplayer2.Timeline;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
@ -1074,70 +1080,60 @@ public final class MediaSessionConnector {
|
|||||||
// Player.EventListener implementation.
|
// Player.EventListener implementation.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
|
public void onEvents(Player player, Player.Events events) {
|
||||||
Player player = Assertions.checkNotNull(MediaSessionConnector.this.player);
|
boolean invalidatePlaybackState = false;
|
||||||
int windowCount = player.getCurrentTimeline().getWindowCount();
|
boolean invalidateMetadata = false;
|
||||||
int windowIndex = player.getCurrentWindowIndex();
|
if (events.contains(Player.EVENT_POSITION_DISCONTINUITY)) {
|
||||||
if (queueNavigator != null) {
|
|
||||||
queueNavigator.onTimelineChanged(player);
|
|
||||||
invalidateMediaSessionPlaybackState();
|
|
||||||
} else if (currentWindowCount != windowCount || currentWindowIndex != windowIndex) {
|
|
||||||
// active queue item and queue navigation actions may need to be updated
|
|
||||||
invalidateMediaSessionPlaybackState();
|
|
||||||
}
|
|
||||||
currentWindowCount = windowCount;
|
|
||||||
currentWindowIndex = windowIndex;
|
|
||||||
invalidateMediaSessionMetadata();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPlaybackStateChanged(@Player.State int playbackState) {
|
|
||||||
invalidateMediaSessionPlaybackState();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPlayWhenReadyChanged(
|
|
||||||
boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
|
|
||||||
invalidateMediaSessionPlaybackState();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onIsPlayingChanged(boolean isPlaying) {
|
|
||||||
invalidateMediaSessionPlaybackState();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onRepeatModeChanged(@Player.RepeatMode int repeatMode) {
|
|
||||||
invalidateMediaSessionPlaybackState();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
|
|
||||||
invalidateMediaSessionPlaybackState();
|
|
||||||
invalidateMediaSessionQueue();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) {
|
|
||||||
Player player = Assertions.checkNotNull(MediaSessionConnector.this.player);
|
|
||||||
if (currentWindowIndex != player.getCurrentWindowIndex()) {
|
if (currentWindowIndex != player.getCurrentWindowIndex()) {
|
||||||
if (queueNavigator != null) {
|
if (queueNavigator != null) {
|
||||||
queueNavigator.onCurrentWindowIndexChanged(player);
|
queueNavigator.onCurrentWindowIndexChanged(player);
|
||||||
}
|
}
|
||||||
currentWindowIndex = player.getCurrentWindowIndex();
|
invalidateMetadata = true;
|
||||||
// Update playback state after queueNavigator.onCurrentWindowIndexChanged has been called
|
|
||||||
// and before updating metadata.
|
|
||||||
invalidateMediaSessionPlaybackState();
|
|
||||||
invalidateMediaSessionMetadata();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
invalidateMediaSessionPlaybackState();
|
invalidatePlaybackState = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
if (events.contains(Player.EVENT_TIMELINE_CHANGED)) {
|
||||||
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
|
int windowCount = player.getCurrentTimeline().getWindowCount();
|
||||||
|
int windowIndex = player.getCurrentWindowIndex();
|
||||||
|
if (queueNavigator != null) {
|
||||||
|
queueNavigator.onTimelineChanged(player);
|
||||||
|
invalidatePlaybackState = true;
|
||||||
|
} else if (currentWindowCount != windowCount || currentWindowIndex != windowIndex) {
|
||||||
|
// active queue item and queue navigation actions may need to be updated
|
||||||
|
invalidatePlaybackState = true;
|
||||||
|
}
|
||||||
|
currentWindowCount = windowCount;
|
||||||
|
invalidateMetadata = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update currentWindowIndex after comparisons above.
|
||||||
|
currentWindowIndex = player.getCurrentWindowIndex();
|
||||||
|
|
||||||
|
if (events.containsAny(
|
||||||
|
EVENT_PLAYBACK_STATE_CHANGED,
|
||||||
|
EVENT_PLAY_WHEN_READY_CHANGED,
|
||||||
|
EVENT_IS_PLAYING_CHANGED,
|
||||||
|
EVENT_REPEAT_MODE_CHANGED,
|
||||||
|
EVENT_PLAYBACK_PARAMETERS_CHANGED)) {
|
||||||
|
invalidatePlaybackState = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The queue needs to be updated by the queue navigator first. The queue navigator also
|
||||||
|
// delivers the active queue item that is used to update the playback state.
|
||||||
|
if (events.containsAny(EVENT_SHUFFLE_MODE_ENABLED_CHANGED)) {
|
||||||
|
invalidateMediaSessionQueue();
|
||||||
|
invalidatePlaybackState = true;
|
||||||
|
}
|
||||||
|
// Invalidate the playback state before invalidating metadata because the active queue item of
|
||||||
|
// the session playback state needs to be updated before the MediaMetadataProvider uses it.
|
||||||
|
if (invalidatePlaybackState) {
|
||||||
invalidateMediaSessionPlaybackState();
|
invalidateMediaSessionPlaybackState();
|
||||||
}
|
}
|
||||||
|
if (invalidateMetadata) {
|
||||||
|
invalidateMediaSessionMetadata();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MediaSessionCompat.Callback implementation.
|
// MediaSessionCompat.Callback implementation.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user