diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index b016d7b29..d54022aa7 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -440,12 +440,16 @@ public abstract class BasePlayer implements Player.EventListener, if (simpleExoPlayer != null) simpleExoPlayer.setVolume(DUCK_AUDIO_TO); animateAudio(DUCK_AUDIO_TO, 1f, DUCK_DURATION); - if (isResumeAfterAudioFocusGain()) simpleExoPlayer.setPlayWhenReady(true); + if (isResumeAfterAudioFocusGain()) { + simpleExoPlayer.setPlayWhenReady(true); + wasPlaying = true; + } } protected void onAudioFocusLoss() { if (DEBUG) Log.d(TAG, "onAudioFocusLoss() called"); simpleExoPlayer.setPlayWhenReady(false); + wasPlaying = false; } protected void onAudioFocusLossCanDuck() { @@ -758,6 +762,7 @@ public abstract class BasePlayer implements Player.EventListener, else playQueue.setIndex(0); } simpleExoPlayer.setPlayWhenReady(!isPlaying()); + wasPlaying = simpleExoPlayer.getPlayWhenReady(); } public void onFastRewind() { @@ -774,10 +779,12 @@ public abstract class BasePlayer implements Player.EventListener, if (simpleExoPlayer == null || playQueue == null || currentInfo == null) return; if (DEBUG) Log.d(TAG, "onPlayPrevious() called"); - if (simpleExoPlayer.getCurrentPosition() <= PLAY_PREV_ACTIVATION_LIMIT) { - playQueue.offsetIndex(-1); - } else { + /* If current playback has run for PLAY_PREV_ACTIVATION_LIMIT milliseconds, restart current track. + * Also restart the track if the current track is the first in a queue.*/ + if (simpleExoPlayer.getCurrentPosition() > PLAY_PREV_ACTIVATION_LIMIT || playQueue.getIndex() == 0) { simpleExoPlayer.seekTo(currentInfo.start_position); + } else { + playQueue.offsetIndex(-1); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/playback/PlaybackManager.java b/app/src/main/java/org/schabi/newpipe/player/playback/PlaybackManager.java index a2dc1ec41..5ffa90b74 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playback/PlaybackManager.java +++ b/app/src/main/java/org/schabi/newpipe/player/playback/PlaybackManager.java @@ -211,18 +211,18 @@ public class PlaybackManager { // The current item has higher priority final int currentIndex = playQueue.getIndex(); final PlayQueueItem currentItem = playQueue.get(currentIndex); - if (currentItem != null) load(currentItem); - else return; + if (currentItem == null) return; + load(currentItem); + + // Load boundaries to ensure correct looping + if (sourceToQueueIndex.indexOf(0) == -1) load(playQueue.get(0)); + if (sourceToQueueIndex.indexOf(playQueue.size() - 1) == -1) load(playQueue.get(playQueue.size() - 1)); // The rest are just for seamless playback 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 int rightBound = Math.min(playQueue.size(), currentIndex + WINDOW_SIZE + 1); final List items = new ArrayList<>(playQueue.getStreams().subList(leftBound, rightBound)); - final int excess = rightLimit - playQueue.size(); - if (excess >= 0) items.addAll(playQueue.getStreams().subList(0, excess)); - for (final PlayQueueItem item: items) load(item); } @@ -245,7 +245,7 @@ public class PlaybackManager { final MediaSource source = playbackListener.sourceOf(streamInfo, item.getSortedQualityIndex()); final int itemIndex = playQueue.indexOf(item); // replace all except the currently playing - insert(itemIndex, source, false); + insert(itemIndex, source, itemIndex != playQueue.getIndex()); if (tryUnblock()) sync(); }