Make DynamicConcatenatingMediaSource reusable.

Issue:#3498

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=183648419
This commit is contained in:
tonihei 2018-01-29 04:53:24 -08:00 committed by Oliver Woodman
parent 1f6d161d4d
commit 72d99284c1
3 changed files with 28 additions and 3 deletions

View File

@ -333,7 +333,6 @@ public final class DynamicConcatenatingMediaSource extends CompositeMediaSource<
public synchronized void prepareSource(ExoPlayer player, boolean isTopLevelSource,
Listener listener) {
super.prepareSource(player, isTopLevelSource, listener);
Assertions.checkState(this.listener == null, MEDIA_SOURCE_REUSED_ERROR_MESSAGE);
this.player = player;
this.listener = listener;
preventListenerNotification = true;
@ -372,6 +371,17 @@ public final class DynamicConcatenatingMediaSource extends CompositeMediaSource<
}
}
@Override
public void releaseSource() {
super.releaseSource();
mediaSourceHolders.clear();
player = null;
listener = null;
shuffleOrder = shuffleOrder.cloneAndClear();
windowCount = 0;
periodCount = 0;
}
@Override
protected void onChildSourceInfoRefreshed(
MediaSourceHolder mediaSourceHolder,

View File

@ -136,6 +136,11 @@ public interface ShuffleOrder {
return new DefaultShuffleOrder(newShuffled, new Random(random.nextLong()));
}
@Override
public ShuffleOrder cloneAndClear() {
return new DefaultShuffleOrder(/* length= */ 0, new Random(random.nextLong()));
}
private static int[] createShuffledList(int length, Random random) {
int[] shuffled = new int[length];
for (int i = 0; i < length; i++) {
@ -199,6 +204,10 @@ public interface ShuffleOrder {
return new UnshuffledShuffleOrder(length - 1);
}
@Override
public ShuffleOrder cloneAndClear() {
return new UnshuffledShuffleOrder(/* length= */ 0);
}
}
/**
@ -237,7 +246,7 @@ public interface ShuffleOrder {
int getFirstIndex();
/**
* Return a copy of the shuffle order with newly inserted elements.
* Returns a copy of the shuffle order with newly inserted elements.
*
* @param insertionIndex The index in the unshuffled order at which elements are inserted.
* @param insertionCount The number of elements inserted at {@code insertionIndex}.
@ -246,11 +255,13 @@ public interface ShuffleOrder {
ShuffleOrder cloneAndInsert(int insertionIndex, int insertionCount);
/**
* Return a copy of the shuffle order with one element removed.
* Returns a copy of the shuffle order with one element removed.
*
* @param removalIndex The index of the element in the unshuffled order which is to be removed.
* @return A copy of this {@link ShuffleOrder} without the removed element.
*/
ShuffleOrder cloneAndRemove(int removalIndex);
/** Returns a copy of the shuffle order with all elements removed. */
ShuffleOrder cloneAndClear();
}

View File

@ -65,4 +65,8 @@ public final class FakeShuffleOrder implements ShuffleOrder {
return new FakeShuffleOrder(length - 1);
}
@Override
public ShuffleOrder cloneAndClear() {
return new FakeShuffleOrder(/* length= */ 0);
}
}