Skip to content

Commit 1d8ea01

Browse files
AudricVStypox
authored andcommitted
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.
1 parent 4648cac commit 1d8ea01

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

app/src/main/java/org/schabi/newpipe/player/Player.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
import org.schabi.newpipe.player.resolver.AudioPlaybackResolver;
115115
import org.schabi.newpipe.player.resolver.VideoPlaybackResolver;
116116
import org.schabi.newpipe.player.resolver.VideoPlaybackResolver.SourceType;
117+
import org.schabi.newpipe.player.ui.BackgroundPlayerUi;
117118
import org.schabi.newpipe.player.ui.MainPlayerUi;
118119
import org.schabi.newpipe.player.ui.PlayerUi;
119120
import org.schabi.newpipe.player.ui.PlayerUiList;
@@ -271,6 +272,7 @@ public final class Player implements PlaybackListener, Listener {
271272
@NonNull
272273
private final HistoryRecordManager recordManager;
273274

275+
private boolean screenOn = true;
274276

275277
/*//////////////////////////////////////////////////////////////////////////
276278
// Constructor
@@ -592,14 +594,17 @@ private void initUIsForCurrentPlayerType() {
592594
switch (playerType) {
593595
case MAIN:
594596
UIs.destroyAll(PopupPlayerUi.class);
597+
UIs.destroyAll(BackgroundPlayerUi.class);
595598
UIs.addAndPrepare(new MainPlayerUi(this, binding));
596599
break;
597600
case POPUP:
598601
UIs.destroyAll(MainPlayerUi.class);
602+
UIs.destroyAll(BackgroundPlayerUi.class);
599603
UIs.addAndPrepare(new PopupPlayerUi(this, binding));
600604
break;
601605
case AUDIO:
602606
UIs.destroyAll(VideoPlayerUi.class);
607+
UIs.addAndPrepare(new BackgroundPlayerUi(this));
603608
break;
604609
}
605610
}
@@ -842,6 +847,12 @@ private void onBroadcastReceived(final Intent intent) {
842847
case ACTION_SHUFFLE:
843848
toggleShuffleModeEnabled();
844849
break;
850+
case Intent.ACTION_SCREEN_OFF:
851+
screenOn = false;
852+
break;
853+
case Intent.ACTION_SCREEN_ON:
854+
screenOn = true;
855+
break;
845856
case Intent.ACTION_CONFIGURATION_CHANGED:
846857
if (DEBUG) {
847858
Log.d(TAG, "ACTION_CONFIGURATION_CHANGED received");
@@ -2462,4 +2473,11 @@ private int getVideoRendererIndex() {
24622473
.orElse(RENDERER_UNAVAILABLE);
24632474
}
24642475
//endregion
2476+
2477+
/**
2478+
* @return whether the device screen is turned on.
2479+
*/
2480+
public boolean isScreenOn() {
2481+
return screenOn;
2482+
}
24652483
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.schabi.newpipe.player.ui;
2+
3+
import androidx.annotation.NonNull;
4+
5+
import org.schabi.newpipe.player.Player;
6+
7+
/**
8+
* This is not a real UI for the background player, it used to disable fetching video and text
9+
* tracks with it.
10+
*
11+
* <p>
12+
* This allows reducing data usage for manifest sources with demuxed audio and video,
13+
* such as livestreams.
14+
* </p>
15+
*/
16+
public class BackgroundPlayerUi extends PlayerUi {
17+
18+
public BackgroundPlayerUi(@NonNull final Player player) {
19+
super(player);
20+
}
21+
22+
@Override
23+
public void initPlayback() {
24+
super.initPlayback();
25+
26+
// Make sure to disable video and subtitles track types
27+
player.useVideoAndSubtitles(false);
28+
}
29+
}

app/src/main/java/org/schabi/newpipe/player/ui/MainPlayerUi.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ public void initPlayback() {
216216
playQueueAdapter = new PlayQueueAdapter(context,
217217
Objects.requireNonNull(player.getPlayQueue()));
218218
segmentAdapter = new StreamSegmentAdapter(getStreamSegmentListener());
219+
220+
// Make sure video and text tracks are enabled if the user is in the app, in the case user
221+
// switched from background player to main player
222+
player.useVideoAndSubtitles(fragmentIsVisible);
219223
}
220224

221225
@Override

app/src/main/java/org/schabi/newpipe/player/ui/PopupPlayerUi.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ private void initPopupCloseOverlay() {
152152
windowManager.addView(closeOverlayBinding.getRoot(), closeOverlayLayoutParams);
153153
}
154154

155+
@Override
156+
public void initPlayback() {
157+
super.initPlayback();
158+
// Make sure video and text tracks are enabled if the screen is turned on (which should
159+
// always be the case), in the case user switched from background player to popup player
160+
player.useVideoAndSubtitles(player.isScreenOn());
161+
}
162+
155163
@Override
156164
protected void setupElementsVisibility() {
157165
binding.fullScreenButton.setVisibility(View.VISIBLE);

0 commit comments

Comments
 (0)