From cf9b482be25b412f183635cb0fc4bfc877c7d035 Mon Sep 17 00:00:00 2001 From: Stypox Date: Tue, 31 Aug 2021 11:10:52 +0200 Subject: [PATCH] Completely close player when changing stream w/o autoplay --- .../fragments/detail/VideoDetailFragment.java | 25 +++++++++++++------ .../org/schabi/newpipe/player/MainPlayer.java | 13 ++++------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 3894fb71b..6e94d2d0d 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -201,7 +201,7 @@ public final class VideoDetailFragment @Nullable private MainPlayer playerService; private Player player; - private PlayerHolder playerHolder = PlayerHolder.getInstance(); + private final PlayerHolder playerHolder = PlayerHolder.getInstance(); /*////////////////////////////////////////////////////////////////////////// // Service management @@ -762,7 +762,7 @@ public final class VideoDetailFragment private void setupFromHistoryItem(final StackItem item) { setAutoPlay(false); - hideMainPlayer(); + hideMainPlayerOnLoadingNewStream(); setInitialData(item.getServiceId(), item.getUrl(), item.getTitle() == null ? "" : item.getTitle(), item.getPlayQueue()); @@ -882,7 +882,7 @@ public final class VideoDetailFragment .observeOn(AndroidSchedulers.mainThread()) .subscribe(result -> { isLoading.set(false); - hideMainPlayer(); + hideMainPlayerOnLoadingNewStream(); if (result.getAgeLimit() != NO_AGE_LIMIT && !prefs.getBoolean( getString(R.string.show_age_restricted_content), false)) { hideAgeRestrictedContent(); @@ -1174,7 +1174,14 @@ public final class VideoDetailFragment ContextCompat.startForegroundService(activity, playerIntent); } - private void hideMainPlayer() { + /** + * When the video detail fragment is already showing details for a video and the user opens a + * new one, the video detail fragment changes all of its old data to the new stream, so if there + * is a video player currently open it should be hidden. This method does exactly that. If + * autoplay is enabled, the underlying player is not stopped completely, since it is going to + * be reused in a few milliseconds and the flickering would be annoying. + */ + private void hideMainPlayerOnLoadingNewStream() { if (!isPlayerServiceAvailable() || playerService.getView() == null || !player.videoPlayerSelected()) { @@ -1182,8 +1189,12 @@ public final class VideoDetailFragment } removeVideoPlayerView(); - playerService.stop(isAutoplayEnabled()); - playerService.getView().setVisibility(View.GONE); + if (isAutoplayEnabled()) { + playerService.stopForImmediateReusing(); + playerService.getView().setVisibility(View.GONE); + } else { + playerHolder.stopService(); + } } private PlayQueue setupPlayQueueForIntent(final boolean append) { @@ -1832,7 +1843,7 @@ public final class VideoDetailFragment || error.type == ExoPlaybackException.TYPE_UNEXPECTED) { // Properly exit from fullscreen toggleFullscreenIfInFullscreenMode(); - hideMainPlayer(); + hideMainPlayerOnLoadingNewStream(); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java index 5b4b308d6..a9b9f4c87 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java @@ -133,32 +133,29 @@ public final class MainPlayer extends Service { return START_NOT_STICKY; } - public void stop(final boolean autoplayEnabled) { + public void stopForImmediateReusing() { if (DEBUG) { - Log.d(TAG, "stop() called"); + Log.d(TAG, "stopForImmediateReusing() called"); } if (!player.exoPlayerIsNull()) { player.saveWasPlaying(); + // Releases wifi & cpu, disables keepScreenOn, etc. - if (!autoplayEnabled) { - player.pause(); - } // We can't just pause the player here because it will make transition // from one stream to a new stream not smooth player.smoothStopPlayer(); player.setRecovery(); + // Android TV will handle back button in case controls will be visible // (one more additional unneeded click while the player is hidden) player.hideControls(0, 0); player.closeItemsList(); + // Notification shows information about old stream but if a user selects // a stream from backStack it's not actual anymore // So we should hide the notification at all. // When autoplay enabled such notification flashing is annoying so skip this case - if (!autoplayEnabled) { - NotificationUtil.getInstance().cancelNotificationAndStopForeground(this); - } } }