diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImpl.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImpl.java index eb26340a737..ab9137cd831 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImpl.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImpl.java @@ -697,7 +697,7 @@ public void moveMediaItems(int fromIndex, int toIndex, int newFromIndex) { } Timeline oldTimeline = getCurrentTimeline(); pendingOperationAcks++; - Util.moveItems(mediaSourceHolderSnapshots, fromIndex, toIndex, newFromIndex); + moveMediaSourceHolders(fromIndex, toIndex, newFromIndex); Timeline newTimeline = createMaskingTimeline(); PlaybackInfo newPlaybackInfo = maskTimelineAndPosition( @@ -719,6 +719,11 @@ public void moveMediaItems(int fromIndex, int toIndex, int newFromIndex) { /* repeatCurrentMediaItem= */ false); } + private void moveMediaSourceHolders(int fromIndex, int toIndex, int newFromIndex) { + Util.moveItems(mediaSourceHolderSnapshots, fromIndex, toIndex, newFromIndex); + shuffleOrder = shuffleOrder.cloneAndMove(fromIndex, toIndex, newFromIndex); + } + @Override public void replaceMediaItems(int fromIndex, int toIndex, List mediaItems) { verifyApplicationThread(); diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ShuffleOrder.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ShuffleOrder.java index e6e2e1b2dcd..47bec157f1e 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ShuffleOrder.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ShuffleOrder.java @@ -266,6 +266,22 @@ public ShuffleOrder cloneAndClear() { */ ShuffleOrder cloneAndInsert(int insertionIndex, int insertionCount); + /** + * Returns a copy of the shuffle order with a range of elements moved. + * + * @param indexFrom The starting index in the unshuffled order of the range to move, from before + * the move occurs. + * @param indexToExclusive The smallest index (must be greater or equal to {@code indexFrom}) that + * is not included in the range of elements moved, from before the move occurs. + * @param newIndexFrom The starting index in the unshuffled order of the range to move, from after + * the move occurs. + * @return A copy of this {@link ShuffleOrder} with the elements moved. + */ + default ShuffleOrder cloneAndMove(int indexFrom, int indexToExclusive, int newIndexFrom) { + return cloneAndRemove(indexFrom, indexToExclusive) + .cloneAndInsert(newIndexFrom, indexToExclusive - indexFrom); + } + /** * Returns a copy of the shuffle order with a range of elements removed. *