diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.java b/app/src/main/java/org/schabi/newpipe/player/Player.java index ef3a8b0fe..b0858f2e3 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -114,6 +114,7 @@ import org.schabi.newpipe.player.playqueue.SinglePlayQueue; import org.schabi.newpipe.player.resolver.AudioPlaybackResolver; import org.schabi.newpipe.player.resolver.VideoPlaybackResolver; import org.schabi.newpipe.player.resolver.VideoPlaybackResolver.SourceType; +import org.schabi.newpipe.player.ui.BackgroundPlayerUi; import org.schabi.newpipe.player.ui.MainPlayerUi; import org.schabi.newpipe.player.ui.PlayerUi; import org.schabi.newpipe.player.ui.PlayerUiList; @@ -271,6 +272,7 @@ public final class Player implements PlaybackListener, Listener { @NonNull private final HistoryRecordManager recordManager; + private boolean screenOn = true; /*////////////////////////////////////////////////////////////////////////// // Constructor @@ -592,14 +594,17 @@ public final class Player implements PlaybackListener, Listener { switch (playerType) { case MAIN: UIs.destroyAll(PopupPlayerUi.class); + UIs.destroyAll(BackgroundPlayerUi.class); UIs.addAndPrepare(new MainPlayerUi(this, binding)); break; case POPUP: UIs.destroyAll(MainPlayerUi.class); + UIs.destroyAll(BackgroundPlayerUi.class); UIs.addAndPrepare(new PopupPlayerUi(this, binding)); break; case AUDIO: UIs.destroyAll(VideoPlayerUi.class); + UIs.addAndPrepare(new BackgroundPlayerUi(this)); break; } } @@ -842,6 +847,12 @@ public final class Player implements PlaybackListener, Listener { case ACTION_SHUFFLE: toggleShuffleModeEnabled(); break; + case Intent.ACTION_SCREEN_OFF: + screenOn = false; + break; + case Intent.ACTION_SCREEN_ON: + screenOn = true; + break; case Intent.ACTION_CONFIGURATION_CHANGED: if (DEBUG) { Log.d(TAG, "ACTION_CONFIGURATION_CHANGED received"); @@ -2462,4 +2473,11 @@ public final class Player implements PlaybackListener, Listener { .orElse(RENDERER_UNAVAILABLE); } //endregion + + /** + * @return whether the device screen is turned on. + */ + public boolean isScreenOn() { + return screenOn; + } } diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/BackgroundPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/ui/BackgroundPlayerUi.java new file mode 100644 index 000000000..9530033d1 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/ui/BackgroundPlayerUi.java @@ -0,0 +1,29 @@ +package org.schabi.newpipe.player.ui; + +import androidx.annotation.NonNull; + +import org.schabi.newpipe.player.Player; + +/** + * This is not a real UI for the background player, it used to disable fetching video and text + * tracks with it. + * + *
+ * This allows reducing data usage for manifest sources with demuxed audio and video, + * such as livestreams. + *
+ */ +public class BackgroundPlayerUi extends PlayerUi { + + public BackgroundPlayerUi(@NonNull final Player player) { + super(player); + } + + @Override + public void initPlayback() { + super.initPlayback(); + + // Make sure to disable video and subtitles track types + player.useVideoAndSubtitles(false); + } +} diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java index b21a387a9..868881782 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java @@ -216,6 +216,10 @@ public final class MainPlayerUi extends VideoPlayerUi implements View.OnLayoutCh playQueueAdapter = new PlayQueueAdapter(context, Objects.requireNonNull(player.getPlayQueue())); segmentAdapter = new StreamSegmentAdapter(getStreamSegmentListener()); + + // Make sure video and text tracks are enabled if the user is in the app, in the case user + // switched from background player to main player + player.useVideoAndSubtitles(fragmentIsVisible); } @Override diff --git a/app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java index 10a982bf0..b9c29c008 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java @@ -152,6 +152,14 @@ public final class PopupPlayerUi extends VideoPlayerUi { windowManager.addView(closeOverlayBinding.getRoot(), closeOverlayLayoutParams); } + @Override + public void initPlayback() { + super.initPlayback(); + // Make sure video and text tracks are enabled if the screen is turned on (which should + // always be the case), in the case user switched from background player to popup player + player.useVideoAndSubtitles(player.isScreenOn()); + } + @Override protected void setupElementsVisibility() { binding.fullScreenButton.setVisibility(View.VISIBLE);