Skip to content

Commit 268ae39

Browse files
committed
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 ef47d6a commit 268ae39

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
@@ -112,6 +112,7 @@
112112
import org.schabi.newpipe.player.resolver.AudioPlaybackResolver;
113113
import org.schabi.newpipe.player.resolver.VideoPlaybackResolver;
114114
import org.schabi.newpipe.player.resolver.VideoPlaybackResolver.SourceType;
115+
import org.schabi.newpipe.player.ui.BackgroundPlayerUi;
115116
import org.schabi.newpipe.player.ui.MainPlayerUi;
116117
import org.schabi.newpipe.player.ui.PlayerUi;
117118
import org.schabi.newpipe.player.ui.PlayerUiList;
@@ -265,6 +266,7 @@ public final class Player implements PlaybackListener, Listener {
265266
@NonNull
266267
private final HistoryRecordManager recordManager;
267268

269+
private boolean screenOn = true;
268270

269271
/*//////////////////////////////////////////////////////////////////////////
270272
// Constructor
@@ -500,14 +502,17 @@ private void initUIsForCurrentPlayerType() {
500502
switch (playerType) {
501503
case MAIN:
502504
UIs.destroyAll(PopupPlayerUi.class);
505+
UIs.destroyAll(BackgroundPlayerUi.class);
503506
UIs.addAndPrepare(new MainPlayerUi(this, binding));
504507
break;
505508
case POPUP:
506509
UIs.destroyAll(MainPlayerUi.class);
510+
UIs.destroyAll(BackgroundPlayerUi.class);
507511
UIs.addAndPrepare(new PopupPlayerUi(this, binding));
508512
break;
509513
case AUDIO:
510514
UIs.destroyAll(VideoPlayerUi.class);
515+
UIs.addAndPrepare(new BackgroundPlayerUi(this));
511516
break;
512517
}
513518
}
@@ -752,6 +757,12 @@ private void onBroadcastReceived(final Intent intent) {
752757
case ACTION_SHUFFLE:
753758
toggleShuffleModeEnabled();
754759
break;
760+
case Intent.ACTION_SCREEN_OFF:
761+
screenOn = false;
762+
break;
763+
case Intent.ACTION_SCREEN_ON:
764+
screenOn = true;
765+
break;
755766
case Intent.ACTION_CONFIGURATION_CHANGED:
756767
if (DEBUG) {
757768
Log.d(TAG, "ACTION_CONFIGURATION_CHANGED received");
@@ -2362,4 +2373,11 @@ private int getVideoRendererIndex() {
23622373
.orElse(RENDERER_UNAVAILABLE);
23632374
}
23642375
//endregion
2376+
2377+
/**
2378+
* @return whether the device screen is turned on.
2379+
*/
2380+
public boolean isScreenOn() {
2381+
return screenOn;
2382+
}
23652383
}
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
@@ -151,6 +151,14 @@ private void initPopupCloseOverlay() {
151151
windowManager.addView(closeOverlayBinding.getRoot(), closeOverlayLayoutParams);
152152
}
153153

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

0 commit comments

Comments
 (0)