Ensure that ShuffleOrder has the same length as the current playlist

Add a fail-fast check in `ExoPlayerImpl` to ensure the equality of the lengths of `ShuffleOrder` and the current playlist. Also improve the documentation of `setShuffleOrder(ShuffleOrder)` with explicit instruction on this.

Issue: androidx/media#480

#minor-release

PiperOrigin-RevId: 544009359
This commit is contained in:
tianyifeng 2023-06-28 10:41:49 +00:00 committed by Tianyi Feng
parent fd284d7de2
commit d895a46b28
3 changed files with 13 additions and 0 deletions

View File

@ -1494,6 +1494,9 @@ public interface ExoPlayer extends Player {
/**
* Sets the shuffle order.
*
* <p>The {@link ShuffleOrder} passed must have the same length as the current playlist ({@link
* Player#getMediaItemCount()}).
*
* @param shuffleOrder The shuffle order.
*/
@UnstableApi

View File

@ -776,6 +776,7 @@ import java.util.concurrent.TimeoutException;
@Override
public void setShuffleOrder(ShuffleOrder shuffleOrder) {
verifyApplicationThread();
checkArgument(shuffleOrder.getLength() == mediaSourceHolderSnapshots.size());
this.shuffleOrder = shuffleOrder;
Timeline timeline = createMaskingTimeline();
PlaybackInfo newPlaybackInfo =

View File

@ -6961,6 +6961,15 @@ public final class ExoPlayerTest {
assertThat(capturedTimelineShuffleIndexes).isEqualTo(newShuffleOrderIndexes);
}
@Test
public void setShuffleOrder_shuffleOrderLengthNotEqualToCurrentPlaylistLength_shouldThrow() {
ExoPlayer player = new TestExoPlayerBuilder(context).build();
player.addMediaSources(ImmutableList.of(new FakeMediaSource(), new FakeMediaSource()));
assertThrows(
IllegalArgumentException.class, () -> player.setShuffleOrder(new FakeShuffleOrder(3)));
}
@Test
public void setMediaSources_empty_whenEmpty_correctMaskingMediaItemIndex() throws Exception {
final int[] currentMediaItemIndices = {C.INDEX_UNSET, C.INDEX_UNSET, C.INDEX_UNSET};