diff --git a/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java b/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java index 8f91e53c2..439885e58 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java +++ b/app/src/main/java/org/schabi/newpipe/player/playback/MediaSourceManager.java @@ -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 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; diff --git a/app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java b/app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java index 9005ef8d2..8f4c5913d 100644 --- a/app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java +++ b/app/src/main/java/org/schabi/newpipe/playlist/PlayQueue.java @@ -45,7 +45,7 @@ public abstract class PlayQueue implements Serializable { private ArrayList backup; private ArrayList streams; - private final AtomicInteger queueIndex; + @NonNull private final AtomicInteger queueIndex; private transient BehaviorSubject eventBroadcast; private transient Flowable 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 items) { + public synchronized void append(@NonNull final List items) { List 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); } diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index da1019f99..4fc854416 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -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,