diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index dfbb98b95..443da74d4 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -378,6 +378,7 @@ public final class BackgroundPlayer extends Service { @Override public void sync(@NonNull final PlayQueueItem item, @Nullable final StreamInfo info) { + if (currentItem == item && currentInfo == info) return; super.sync(item, info); resetNotification(); diff --git a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java index c275e55a7..b91a0814b 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java @@ -48,6 +48,7 @@ import com.google.android.exoplayer2.Player; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.VideoStream; +import org.schabi.newpipe.fragments.OnScrollBelowItemsListener; import org.schabi.newpipe.player.helper.PlayerHelper; import org.schabi.newpipe.playlist.PlayQueueItem; import org.schabi.newpipe.playlist.PlayQueueItemBuilder; @@ -397,7 +398,7 @@ public final class MainVideoPlayer extends Activity { getControlsRoot().setVisibility(View.INVISIBLE); queueLayout.setVisibility(View.VISIBLE); - itemsList.smoothScrollToPosition(playQueue.getIndex()); + itemsList.scrollToPosition(playQueue.getIndex()); } private void onQueueClosed() { @@ -565,6 +566,9 @@ public final class MainVideoPlayer extends Activity { itemsList.setClickable(true); itemsList.setLongClickable(true); + itemsList.clearOnScrollListeners(); + itemsList.addOnScrollListener(getQueueScrollListener()); + itemTouchHelper = new ItemTouchHelper(getItemTouchCallback()); itemTouchHelper.attachToRecyclerView(itemsList); @@ -578,6 +582,19 @@ public final class MainVideoPlayer extends Activity { }); } + private OnScrollBelowItemsListener getQueueScrollListener() { + return new OnScrollBelowItemsListener() { + @Override + public void onScrolledDown(RecyclerView recyclerView) { + if (playQueue != null && !playQueue.isComplete()) { + playQueue.fetch(); + } else if (itemsList != null) { + itemsList.clearOnScrollListeners(); + } + } + }; + } + private ItemTouchHelper.SimpleCallback getItemTouchCallback() { return new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0) { @Override diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java index babbdbff5..48a435d37 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java @@ -573,6 +573,7 @@ public final class PopupVideoPlayer extends Service { @Override public void sync(@NonNull PlayQueueItem item, @Nullable StreamInfo info) { + if (currentItem == item && currentInfo == info) return; super.sync(item, info); updateMetadata(); } diff --git a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java index 4fe228dfc..98595b358 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java @@ -28,6 +28,7 @@ import com.google.android.exoplayer2.Player; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.stream.StreamInfo; +import org.schabi.newpipe.fragments.OnScrollBelowItemsListener; import org.schabi.newpipe.player.event.PlayerEventListener; import org.schabi.newpipe.playlist.PlayQueueItem; import org.schabi.newpipe.playlist.PlayQueueItemBuilder; @@ -57,6 +58,8 @@ public abstract class ServicePlayerActivity extends AppCompatActivity private static final int PLAYBACK_SPEED_POPUP_MENU_GROUP_ID = 61; private static final int PLAYBACK_PITCH_POPUP_MENU_GROUP_ID = 97; + private static final int SMOOTH_SCROLL_MAXIMUM_DISTANCE = 80; + private View rootView; private RecyclerView itemsList; @@ -225,6 +228,8 @@ public abstract class ServicePlayerActivity extends AppCompatActivity itemsList.setAdapter(player.getPlayQueueAdapter()); itemsList.setClickable(true); itemsList.setLongClickable(true); + itemsList.clearOnScrollListeners(); + itemsList.addOnScrollListener(getQueueScrollListener()); itemTouchHelper = new ItemTouchHelper(getItemTouchCallback()); itemTouchHelper.attachToRecyclerView(itemsList); @@ -345,6 +350,19 @@ public abstract class ServicePlayerActivity extends AppCompatActivity // Component Helpers //////////////////////////////////////////////////////////////////////////// + private OnScrollBelowItemsListener getQueueScrollListener() { + return new OnScrollBelowItemsListener() { + @Override + public void onScrolledDown(RecyclerView recyclerView) { + if (player != null && player.getPlayQueue() != null && !player.getPlayQueue().isComplete()) { + player.getPlayQueue().fetch(); + } else if (itemsList != null) { + itemsList.clearOnScrollListeners(); + } + } + }; + } + private ItemTouchHelper.SimpleCallback getItemTouchCallback() { return new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0) { @Override @@ -401,7 +419,23 @@ public abstract class ServicePlayerActivity extends AppCompatActivity } private void scrollToSelected() { - itemsList.smoothScrollToPosition(player.getPlayQueue().getIndex()); + if (player == null) return; + + final int currentPlayingIndex = player.getPlayQueue().getIndex(); + final int currentVisibleIndex; + if (itemsList.getLayoutManager() instanceof LinearLayoutManager) { + final LinearLayoutManager layout = ((LinearLayoutManager) itemsList.getLayoutManager()); + currentVisibleIndex = layout.findFirstVisibleItemPosition(); + } else { + currentVisibleIndex = 0; + } + + final int distance = Math.abs(currentPlayingIndex - currentVisibleIndex); + if (distance < SMOOTH_SCROLL_MAXIMUM_DISTANCE) { + itemsList.smoothScrollToPosition(currentPlayingIndex); + } else { + itemsList.scrollToPosition(currentPlayingIndex); + } } //////////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/java/org/schabi/newpipe/playlist/AbstractInfoPlayQueue.java b/app/src/main/java/org/schabi/newpipe/playlist/AbstractInfoPlayQueue.java index 74a4dc555..b35271b8f 100644 --- a/app/src/main/java/org/schabi/newpipe/playlist/AbstractInfoPlayQueue.java +++ b/app/src/main/java/org/schabi/newpipe/playlist/AbstractInfoPlayQueue.java @@ -30,10 +30,10 @@ abstract class AbstractInfoPlayQueue ext } AbstractInfoPlayQueue(final int serviceId, - final String url, - final String nextPageUrl, - final List streams, - final int index) { + final String url, + final String nextPageUrl, + final List streams, + final int index) { super(index, extractListItems(streams)); this.baseUrl = url;