Skip to content

Commit 4ef2a2f

Browse files
nift4tianyif
authored andcommitted
add ShuffleOrder.cloneAndSet() to provide startIndex to shuffle order
Shuffle order may want to ensure that the song that's going to be played (was chosen by user) is also the first song in shuffle playlist, as that makes sure all other songs will be playing afterwards. If first song gets random position in shuffled playlist, playback may stop before playing every song as it will start in the middle of shuffled list.
1 parent 05227c9 commit 4ef2a2f

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImpl.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,12 +2513,8 @@ private void setMediaSourcesInternal(
25132513
int currentWindowIndex = getCurrentWindowIndexInternal(playbackInfo);
25142514
long currentPositionMs = getCurrentPosition();
25152515
pendingOperationAcks++;
2516-
if (!mediaSourceHolderSnapshots.isEmpty()) {
2517-
removeMediaSourceHolders(
2518-
/* fromIndex= */ 0, /* toIndexExclusive= */ mediaSourceHolderSnapshots.size());
2519-
}
25202516
List<MediaSourceList.MediaSourceHolder> holders =
2521-
addMediaSourceHolders(/* index= */ 0, mediaSources);
2517+
setMediaSourceHolders(mediaSources, startWindowIndex);
25222518
Timeline timeline = createMaskingTimeline();
25232519
if (!timeline.isEmpty() && startWindowIndex >= timeline.getWindowCount()) {
25242520
throw new IllegalSeekPositionException(timeline, startWindowIndex, startPositionMs);
@@ -2564,6 +2560,21 @@ private void setMediaSourcesInternal(
25642560
/* repeatCurrentMediaItem= */ false);
25652561
}
25662562

2563+
private List<MediaSourceList.MediaSourceHolder> setMediaSourceHolders(
2564+
List<MediaSource> mediaSources, int startIndex) {
2565+
mediaSourceHolderSnapshots.clear();
2566+
List<MediaSourceList.MediaSourceHolder> holders = new ArrayList<>();
2567+
for (int i = 0; i < mediaSources.size(); i++) {
2568+
MediaSourceList.MediaSourceHolder holder =
2569+
new MediaSourceList.MediaSourceHolder(mediaSources.get(i), useLazyPreparation);
2570+
holders.add(holder);
2571+
mediaSourceHolderSnapshots.add(
2572+
i, new MediaSourceHolderSnapshot(holder.uid, holder.mediaSource));
2573+
}
2574+
shuffleOrder = shuffleOrder.cloneAndSet(/* insertionCount= */ holders.size(), startIndex);
2575+
return holders;
2576+
}
2577+
25672578
private List<MediaSourceList.MediaSourceHolder> addMediaSourceHolders(
25682579
int index, List<MediaSource> mediaSources) {
25692580
List<MediaSourceList.MediaSourceHolder> holders = new ArrayList<>();

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/source/ShuffleOrder.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,25 @@ default ShuffleOrder cloneAndMove(int indexFrom, int indexToExclusive, int newIn
284284
return this;
285285
}
286286

287+
/**
288+
* Returns a copy of the shuffle order with all elements replaced.
289+
*
290+
* <p>The default implementation uses {@link #cloneAndClear} and {@link #cloneAndInsert(int, int)}
291+
* to replace all elements in the shuffle order. Custom implementations can override this method
292+
* if the first element in the shuffled order should be set to the one whose index in the
293+
* unshuffled order is {@code startIndex}.
294+
*
295+
* @param insertionCount The number of elements.
296+
* @param startIndex The index of the new element in the unshuffled order that should be the first
297+
* in the shuffled order or {@link C#INDEX_UNSET} if the the first element in the shuffled
298+
* order is not specified. It should be ignored if the new list is empty, or if it is larger
299+
* than the last index (inclusive) of the new list.
300+
* @return A copy of this {@link ShuffleOrder} with the elements replaced.
301+
*/
302+
default ShuffleOrder cloneAndSet(int insertionCount, int startIndex) {
303+
return cloneAndClear().cloneAndInsert(0, insertionCount);
304+
}
305+
287306
/**
288307
* Returns a copy of the shuffle order with a range of elements removed.
289308
*

0 commit comments

Comments
 (0)