114114import org .schabi .newpipe .player .resolver .AudioPlaybackResolver ;
115115import org .schabi .newpipe .player .resolver .VideoPlaybackResolver ;
116116import org .schabi .newpipe .player .resolver .VideoPlaybackResolver .SourceType ;
117+ import org .schabi .newpipe .player .ui .BackgroundPlayerUi ;
117118import org .schabi .newpipe .player .ui .MainPlayerUi ;
118119import org .schabi .newpipe .player .ui .PlayerUi ;
119120import 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
@@ -574,6 +576,7 @@ private static PlayQueue getPlayQueueFromCache(@NonNull final Intent intent) {
574576
575577 private void initUIsForCurrentPlayerType () {
576578 if ((UIs .get (MainPlayerUi .class ).isPresent () && playerType == PlayerType .MAIN )
579+ || (UIs .get (BackgroundPlayerUi .class ).isPresent () && playerType == PlayerType .AUDIO )
577580 || (UIs .get (PopupPlayerUi .class ).isPresent () && playerType == PlayerType .POPUP )) {
578581 // correct UI already in place
579582 return ;
@@ -592,14 +595,17 @@ private void initUIsForCurrentPlayerType() {
592595 switch (playerType ) {
593596 case MAIN :
594597 UIs .destroyAll (PopupPlayerUi .class );
598+ UIs .destroyAll (BackgroundPlayerUi .class );
595599 UIs .addAndPrepare (new MainPlayerUi (this , binding ));
596600 break ;
597601 case POPUP :
598602 UIs .destroyAll (MainPlayerUi .class );
603+ UIs .destroyAll (BackgroundPlayerUi .class );
599604 UIs .addAndPrepare (new PopupPlayerUi (this , binding ));
600605 break ;
601606 case AUDIO :
602- UIs .destroyAll (VideoPlayerUi .class );
607+ UIs .destroyAll (VideoPlayerUi .class ); // destroys both MainPlayerUi and PopupPlayerUi
608+ UIs .addAndPrepare (new BackgroundPlayerUi (this ));
603609 break ;
604610 }
605611 }
@@ -842,6 +848,12 @@ private void onBroadcastReceived(final Intent intent) {
842848 case ACTION_SHUFFLE :
843849 toggleShuffleModeEnabled ();
844850 break ;
851+ case Intent .ACTION_SCREEN_OFF :
852+ screenOn = false ;
853+ break ;
854+ case Intent .ACTION_SCREEN_ON :
855+ screenOn = true ;
856+ break ;
845857 case Intent .ACTION_CONFIGURATION_CHANGED :
846858 if (DEBUG ) {
847859 Log .d (TAG , "ACTION_CONFIGURATION_CHANGED received" );
@@ -2195,40 +2207,41 @@ private void notifyAudioTrackUpdateToListeners() {
21952207 }
21962208 }
21972209
2198- public void useVideoSource (final boolean videoEnabled ) {
2199- if (playQueue == null || audioPlayerSelected () ) {
2210+ public void useVideoAndSubtitles (final boolean videoAndSubtitlesEnabled ) {
2211+ if (playQueue == null ) {
22002212 return ;
22012213 }
22022214
2203- isAudioOnly = !videoEnabled ;
2215+ isAudioOnly = !videoAndSubtitlesEnabled ;
22042216
22052217 getCurrentStreamInfo ().ifPresentOrElse (info -> {
22062218 // In case we don't know the source type, fall back to either video-with-audio, or
22072219 // audio-only source type
22082220 final SourceType sourceType = videoResolver .getStreamSourceType ()
22092221 .orElse (SourceType .VIDEO_WITH_AUDIO_OR_AUDIO_ONLY );
22102222
2223+ setRecovery (); // making sure to save playback position before reloadPlayQueueManager()
2224+
22112225 if (playQueueManagerReloadingNeeded (sourceType , info , getVideoRendererIndex ())) {
22122226 reloadPlayQueueManager ();
22132227 }
2214-
2215- setRecovery ();
2216-
2217- // Disable or enable video and subtitles renderers depending of the videoEnabled value
2218- trackSelector .setParameters (trackSelector .buildUponParameters ()
2219- .setTrackTypeDisabled (C .TRACK_TYPE_TEXT , !videoEnabled )
2220- .setTrackTypeDisabled (C .TRACK_TYPE_VIDEO , !videoEnabled ));
22212228 }, () -> {
22222229 /*
22232230 The current metadata may be null sometimes (for e.g. when using an unstable connection
2224- in livestreams) so we will be not able to execute the block below
2231+ in livestreams) so we will be not able to execute the block above
22252232
22262233 Reload the play queue manager in this case, which is the behavior when we don't know the
22272234 index of the video renderer or playQueueManagerReloadingNeeded returns true
22282235 */
2236+ setRecovery (); // making sure to save playback position before reloadPlayQueueManager()
22292237 reloadPlayQueueManager ();
2230- setRecovery ();
22312238 });
2239+
2240+ // Disable or enable video and subtitles renderers depending of the
2241+ // videoAndSubtitlesEnabled value
2242+ trackSelector .setParameters (trackSelector .buildUponParameters ()
2243+ .setTrackTypeDisabled (C .TRACK_TYPE_TEXT , !videoAndSubtitlesEnabled )
2244+ .setTrackTypeDisabled (C .TRACK_TYPE_VIDEO , !videoAndSubtitlesEnabled ));
22322245 }
22332246
22342247 /**
@@ -2461,4 +2474,11 @@ private int getVideoRendererIndex() {
24612474 .orElse (RENDERER_UNAVAILABLE );
24622475 }
24632476 //endregion
2477+
2478+ /**
2479+ * @return whether the device screen is turned on.
2480+ */
2481+ public boolean isScreenOn () {
2482+ return screenOn ;
2483+ }
24642484}
0 commit comments