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 a6f4c75f2..5b2820b4e 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -197,6 +197,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.IntStream; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; import io.reactivex.rxjava3.core.Observable; @@ -4179,8 +4180,8 @@ public final class Player implements // The current metadata may be null sometimes (for e.g. when using an unstable connection // in livestreams) so we will be not able to execute the block below. - // Reload the play queue manager in this case, which is the behavior when the video stream - // played is not video only or when we don't know the index of the video renderer. + // Reload the play queue manager in this case, which is the behavior when we don't know the + // index of the video renderer or playQueueManagerReloadingNeeded returns true. if (currentMetadata == null) { reloadPlayQueueManager(); setRecovery(); @@ -4190,41 +4191,22 @@ public final class Player implements final int videoRenderIndex = getVideoRendererIndex(); final StreamInfo info = currentMetadata.getMetadata(); - /* For video streams: we don't want to stream in background the video stream so if the - video stream played is not a video-only stream and if there is an audio stream available, - play this audio stream in background by reloading the play queue manager. - Otherwise the video renderer will be just disabled (because there is no - other stream for it to play the audio): if the video stream is video-only, only the audio - stream will be fetched and the video stream will be fetched again when the user return to a - video player. + // In the case we don't know the source type, fallback to the one with video with audio or + // audio-only source. + final SourceType sourceType = videoResolver.getStreamSourceType().orElse( + SourceType.VIDEO_WITH_AUDIO_OR_AUDIO_ONLY); - For audio streams and audio live streams: nothing is done, it's not needed to reload the - player with the same audio stream. + if (playQueueManagerReloadingNeeded(sourceType, info, videoRenderIndex)) { + reloadPlayQueueManager(); + } else { + final StreamType streamType = info.getStreamType(); + if (streamType == StreamType.AUDIO_STREAM + || streamType == StreamType.AUDIO_LIVE_STREAM) { + // Nothing to do more than setting the recovery position + setRecovery(); + return; + } - For video live streams: the play queue manager is not reloaded if the stream source is a - live source (see VideoPlaybackResolver#resolve()) and if that's not the case, the - requirements for video streams is applied. - - In the case where we don't know the index of the video renderer, the play queue manager - is also reloaded. */ - - final StreamType streamType = info.getStreamType(); - final SourceType sourceType = videoResolver.getStreamSourceType() - .orElse(SourceType.VIDEO_WITH_SEPARATED_AUDIO); - - final boolean isVideoWithSeparatedAudioOrVideoWithNoSeparatedAudioStreams = - sourceType == SourceType.VIDEO_WITH_SEPARATED_AUDIO - || (sourceType == SourceType.VIDEO_WITH_AUDIO_OR_AUDIO_ONLY - && isNullOrEmpty(info.getAudioStreams())); - final boolean isVideoStreamTypeAndIsVideoOnlyStreamOrNoAudioStreamsAvailable = - streamType == StreamType.VIDEO_STREAM - && isVideoWithSeparatedAudioOrVideoWithNoSeparatedAudioStreams - || (streamType == StreamType.LIVE_STREAM - && (sourceType == SourceType.LIVE_STREAM - || isVideoWithSeparatedAudioOrVideoWithNoSeparatedAudioStreams)); - - if (videoRenderIndex != RENDERER_UNAVAILABLE - && isVideoStreamTypeAndIsVideoOnlyStreamOrNoAudioStreamsAvailable) { final TrackGroupArray videoTrackGroupArray = Objects.requireNonNull( trackSelector.getCurrentMappedTrackInfo()).getTrackGroups(videoRenderIndex); if (videoEnabled) { @@ -4238,13 +4220,72 @@ public final class Player implements trackSelector.setParameters(trackSelector.buildUponParameters() .setSelectionOverride(videoRenderIndex, videoTrackGroupArray, null)); } - } else if (streamType != StreamType.AUDIO_STREAM - && streamType != StreamType.AUDIO_LIVE_STREAM) { - reloadPlayQueueManager(); } setRecovery(); } + + /** + * Return whether the play queue manager needs to be reloaded when switching player type. + * + *
+ * The play queue manager needs to be reloaded if the video renderer index is not known and if + * the content is not an audio content, but also if none of the following cases is met: + * + *