Disable fetching video and text tracks in background player

This reduces data usage for manifest sources with demuxed audio and
video, such as livestreams, for non-HLS sources only due to an
ExoPlayer bug.
This commit is contained in:
AudricV 2025-09-04 17:58:31 +02:00 committed by Stypox
parent 4648cac9c6
commit 1d8ea0181f
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
4 changed files with 59 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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.
*
* <p>
* This allows reducing data usage for manifest sources with demuxed audio and video,
* such as livestreams.
* </p>
*/
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);
}
}

View File

@ -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

View File

@ -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);