diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 1e0325944..4c993a650 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -784,7 +784,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // the app was put somewhere in the background - remember to not automatically continue // playing on re-creation of the app - requireArguments().putBoolean(IntentData.wasIntentStopped, true) + // only run if the re-creation is not caused by an orientation change + if (!viewModel.isOrientationChangeInProgress) { + requireArguments().putBoolean(IntentData.wasIntentStopped, true) + } super.onPause() } @@ -813,7 +816,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { // the player could also be a different instance because a new player fragment // got created in the meanwhile - if (!viewModel.shouldUseExistingPlayer && viewModel.player == exoPlayer) { + if (!viewModel.isOrientationChangeInProgress && viewModel.player == exoPlayer) { viewModel.player = null viewModel.trackSelector = null } @@ -848,7 +851,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { viewModel.nowPlayingNotification?.destroySelf() viewModel.nowPlayingNotification = null - if (!viewModel.shouldUseExistingPlayer) { + if (!viewModel.isOrientationChangeInProgress) { exoPlayer.stop() exoPlayer.release() } @@ -931,7 +934,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { binding.sbSkipBtn.isGone = true // set media sources for the player - if (!viewModel.shouldUseExistingPlayer) initStreamSources() + if (!viewModel.isOrientationChangeInProgress) initStreamSources() if (PreferenceHelper.getBoolean(PreferenceKeys.AUTO_FULLSCREEN_SHORTS, false) && isShort && binding.playerMotionLayout.progress == 0f @@ -990,7 +993,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { exoPlayer.setPlaybackSpeed(1f) } - viewModel.shouldUseExistingPlayer = false + viewModel.isOrientationChangeInProgress = false } } @@ -1670,7 +1673,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { } playerLayoutOrientation = orientation - viewModel.shouldUseExistingPlayer = true + viewModel.isOrientationChangeInProgress = true activity?.recreate() } } diff --git a/app/src/main/java/com/github/libretube/ui/models/PlayerViewModel.kt b/app/src/main/java/com/github/libretube/ui/models/PlayerViewModel.kt index 180c09c1e..cf0dddb7f 100644 --- a/app/src/main/java/com/github/libretube/ui/models/PlayerViewModel.kt +++ b/app/src/main/java/com/github/libretube/ui/models/PlayerViewModel.kt @@ -37,10 +37,11 @@ class PlayerViewModel : ViewModel() { var sponsorBlockConfig = PlayerHelper.getSponsorBlockCategories() /** - * Whether to continue using the current player + * Whether an orientation change is in progress, so that the current player should be continued to use + * * Set to true if the activity will be recreated due to an orientation change */ - var shouldUseExistingPlayer = false + var isOrientationChangeInProgress = false val isMiniPlayerVisible = MutableLiveData(false) val isFullscreen = MutableLiveData(false) @@ -56,7 +57,7 @@ class PlayerViewModel : ViewModel() { */ suspend fun fetchVideoInfo(context: Context, videoId: String): Pair = withContext(Dispatchers.IO) { - if (shouldUseExistingPlayer && streamsInfo != null) return@withContext streamsInfo to null + if (isOrientationChangeInProgress && streamsInfo != null) return@withContext streamsInfo to null streamsInfo = try { RetrofitInstance.api.getStreams(videoId).apply { @@ -75,7 +76,7 @@ class PlayerViewModel : ViewModel() { } suspend fun fetchSponsorBlockSegments(videoId: String) = withContext(Dispatchers.IO) { - if (sponsorBlockConfig.isEmpty() || shouldUseExistingPlayer) return@withContext + if (sponsorBlockConfig.isEmpty() || isOrientationChangeInProgress) return@withContext runCatching { segments = @@ -88,7 +89,7 @@ class PlayerViewModel : ViewModel() { @OptIn(UnstableApi::class) fun keepOrCreatePlayer(context: Context): Pair { - if (!shouldUseExistingPlayer || player == null || trackSelector == null) { + if (!isOrientationChangeInProgress || player == null || trackSelector == null) { this.trackSelector = DefaultTrackSelector(context) this.player = PlayerHelper.createPlayer(context, trackSelector!!, false) }