From 7aa8a5c368e0142ff0008c9c0c7c876477b8935f Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Wed, 22 Jul 2020 02:20:58 +0300 Subject: [PATCH] Fixed a situation when background playback could use a video stream instead of an audio stream --- .../fragments/detail/VideoDetailFragment.java | 4 +- .../newpipe/player/VideoPlayerImpl.java | 42 ++++++++++++------- 2 files changed, 30 insertions(+), 16 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 a83acd476..c920a103a 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 @@ -2164,10 +2164,10 @@ public class VideoDetailFragment @Override public void onStateChanged(@NonNull final View bottomSheet, final int newState) { bottomSheetState = newState; - ViewGroup mainFragment = requireActivity().findViewById(R.id.fragment_holder); - + final ViewGroup mainFragment = requireActivity().findViewById(R.id.fragment_holder); switch (newState) { case BottomSheetBehavior.STATE_HIDDEN: + mainFragment.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); bottomSheetBehavior.setPeekHeight(0); cleanUp(); break; diff --git a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java index 4eea23a0b..435396fd6 100644 --- a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java +++ b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java @@ -187,6 +187,7 @@ public class VideoPlayerImpl extends VideoPlayer private boolean audioOnly = false; private boolean isFullscreen = false; private boolean isVerticalVideo = false; + private boolean fragmentIsVisible = false; boolean shouldUpdateOnProgress; int timesNotificationUpdated; @@ -1224,28 +1225,22 @@ public class VideoPlayerImpl extends VideoPlayer break; case ACTION_PLAY_PAUSE: onPlayPause(); + if (!fragmentIsVisible) { + // Ensure that we have audio-only stream playing when a user + // started to play from notification's play button from outside of the app + onFragmentStopped(); + } break; case ACTION_REPEAT: onRepeatClicked(); break; case VideoDetailFragment.ACTION_VIDEO_FRAGMENT_RESUMED: + fragmentIsVisible = true; useVideoSource(true); break; case VideoDetailFragment.ACTION_VIDEO_FRAGMENT_STOPPED: - // This will be called when user goes to another app/activity, turns off a screen. - // We don't want to interrupt playback and don't want to see notification - // if player is stopped. - // Next lines of code will enable background playback if needed - if (videoPlayerSelected() && (isPlaying() || isLoading())) { - if (backgroundPlaybackEnabled()) { - useVideoSource(false); - } else if (minimizeOnPopupEnabled()) { - setRecovery(); - NavigationHelper.playOnPopupPlayer(getParentActivity(), playQueue, true); - } else { - onPause(); - } - } + fragmentIsVisible = false; + onFragmentStopped(); break; case Intent.ACTION_CONFIGURATION_CHANGED: assureCorrectAppLanguage(service); @@ -1998,6 +1993,7 @@ public class VideoPlayerImpl extends VideoPlayer public void setFragmentListener(final PlayerServiceEventListener listener) { fragmentListener = listener; + fragmentIsVisible = true; updateMetadata(); updatePlayback(); triggerProgressUpdate(); @@ -2072,6 +2068,24 @@ public class VideoPlayerImpl extends VideoPlayer } } + /** + * This will be called when a user goes to another app/activity, turns off a screen. + * We don't want to interrupt playback and don't want to see notification so + * next lines of code will enable audio-only playback only if needed + * */ + private void onFragmentStopped() { + if (videoPlayerSelected() && (isPlaying() || isLoading())) { + if (backgroundPlaybackEnabled()) { + useVideoSource(false); + } else if (minimizeOnPopupEnabled()) { + setRecovery(); + NavigationHelper.playOnPopupPlayer(getParentActivity(), playQueue, true); + } else { + onPause(); + } + } + } + /////////////////////////////////////////////////////////////////////////// // Getters ///////////////////////////////////////////////////////////////////////////