-Re-added loading for items prior to current index in MediaSourceManager to allow faster access time.

-Added some null checks annotation.
This commit is contained in:
John Zhen Mo 2018-02-28 23:25:45 -08:00
parent a1220c77da
commit 9ea08c8a4b
3 changed files with 22 additions and 13 deletions

View File

@ -326,8 +326,10 @@ public class MediaSourceManager {
maybeLoadItem(currentItem);
// The rest are just for seamless playback
final int leftBound = currentIndex + 1;
final int rightLimit = leftBound + WINDOW_SIZE;
// Although timeline is not updated prior to the current index, these sources are still
// loaded into the cache for faster retrieval at a potentially later time.
final int leftBound = Math.max(0, currentIndex - WINDOW_SIZE);
final int rightLimit = currentIndex + WINDOW_SIZE + 1;
final int rightBound = Math.min(playQueue.size(), rightLimit);
final List<PlayQueueItem> items = new ArrayList<>(
playQueue.getStreams().subList(leftBound,rightBound));
@ -343,10 +345,9 @@ public class MediaSourceManager {
}
}
private void maybeLoadItem(@Nullable final PlayQueueItem item) {
private void maybeLoadItem(@NonNull final PlayQueueItem item) {
if (DEBUG) Log.d(TAG, "maybeLoadItem() called.");
if (sources == null || item == null) return;
if (sources == null) return;
final int index = playQueue.indexOf(item);
if (index > sources.getSize() - 1) return;
@ -355,7 +356,11 @@ public class MediaSourceManager {
if (DEBUG) Log.d(TAG, " Loaded: [" + item.getTitle() +
"] with url: " + item.getUrl());
if (isCorrectionNeeded(item)) update(playQueue.indexOf(item), mediaSource);
final int itemIndex = playQueue.indexOf(item);
// Only update the playlist timeline for items at the current index or after.
if (itemIndex >= playQueue.getIndex() && isCorrectionNeeded(item)) {
update(itemIndex, mediaSource);
}
loadingItems.remove(item);
tryUnblock();
@ -449,7 +454,7 @@ public class MediaSourceManager {
* with position * in respect to the play queue only if no {@link MediaSource}
* already exists at the given index.
* */
private void emplace(final int index, final MediaSource source) {
private void emplace(final int index, @NonNull final MediaSource source) {
if (sources == null) return;
if (index < 0 || index < sources.getSize()) return;
@ -489,7 +494,7 @@ public class MediaSourceManager {
* this will modify the playback timeline prior to the index and cause desynchronization
* on the playing item between {@link PlayQueue} and {@link DynamicConcatenatingMediaSource}.
* */
private synchronized void update(final int index, final MediaSource source) {
private synchronized void update(final int index, @NonNull final MediaSource source) {
if (sources == null) return;
if (index < 0 || index >= sources.getSize()) return;

View File

@ -45,7 +45,7 @@ public abstract class PlayQueue implements Serializable {
private ArrayList<PlayQueueItem> backup;
private ArrayList<PlayQueueItem> streams;
private final AtomicInteger queueIndex;
@NonNull private final AtomicInteger queueIndex;
private transient BehaviorSubject<PlayQueueEvent> eventBroadcast;
private transient Flowable<PlayQueueEvent> broadcastReceiver;
@ -133,7 +133,7 @@ public abstract class PlayQueue implements Serializable {
* Returns the index of the given item using referential equality.
* May be null despite play queue contains identical item.
* */
public int indexOf(final PlayQueueItem item) {
public int indexOf(@NonNull final PlayQueueItem item) {
// referential equality, can't think of a better way to do this
// todo: better than this
return streams.indexOf(item);
@ -213,7 +213,7 @@ public abstract class PlayQueue implements Serializable {
*
* @see #append(List items)
* */
public synchronized void append(final PlayQueueItem... items) {
public synchronized void append(@NonNull final PlayQueueItem... items) {
append(Arrays.asList(items));
}
@ -225,7 +225,7 @@ public abstract class PlayQueue implements Serializable {
*
* Will emit a {@link AppendEvent} on any given context.
* */
public synchronized void append(final List<PlayQueueItem> items) {
public synchronized void append(@NonNull final List<PlayQueueItem> items) {
List<PlayQueueItem> itemList = new ArrayList<>(items);
if (isShuffled()) {
@ -393,7 +393,7 @@ public abstract class PlayQueue implements Serializable {
// Rx Broadcast
//////////////////////////////////////////////////////////////////////////*/
private void broadcast(final PlayQueueEvent event) {
private void broadcast(@NonNull final PlayQueueEvent event) {
if (eventBroadcast != null) {
eventBroadcast.onNext(event);
}

View File

@ -61,6 +61,7 @@ public class NavigationHelper {
// Players
//////////////////////////////////////////////////////////////////////////*/
@NonNull
public static Intent getPlayerIntent(@NonNull final Context context,
@NonNull final Class targetClazz,
@NonNull final PlayQueue playQueue,
@ -74,12 +75,14 @@ public class NavigationHelper {
return intent;
}
@NonNull
public static Intent getPlayerIntent(@NonNull final Context context,
@NonNull final Class targetClazz,
@NonNull final PlayQueue playQueue) {
return getPlayerIntent(context, targetClazz, playQueue, null);
}
@NonNull
public static Intent getPlayerEnqueueIntent(@NonNull final Context context,
@NonNull final Class targetClazz,
@NonNull final PlayQueue playQueue,
@ -89,6 +92,7 @@ public class NavigationHelper {
.putExtra(BasePlayer.SELECT_ON_APPEND, selectOnAppend);
}
@NonNull
public static Intent getPlayerIntent(@NonNull final Context context,
@NonNull final Class targetClazz,
@NonNull final PlayQueue playQueue,