Merge pull request #5750 from Bnyro/master

fix: playback pauses after orientation change
This commit is contained in:
Bnyro 2024-03-17 12:37:15 +01:00 committed by GitHub
commit f8aa66d856
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 11 deletions

View File

@ -784,7 +784,10 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// the app was put somewhere in the background - remember to not automatically continue // the app was put somewhere in the background - remember to not automatically continue
// playing on re-creation of the app // 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() super.onPause()
} }
@ -813,7 +816,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
// the player could also be a different instance because a new player fragment // the player could also be a different instance because a new player fragment
// got created in the meanwhile // got created in the meanwhile
if (!viewModel.shouldUseExistingPlayer && viewModel.player == exoPlayer) { if (!viewModel.isOrientationChangeInProgress && viewModel.player == exoPlayer) {
viewModel.player = null viewModel.player = null
viewModel.trackSelector = null viewModel.trackSelector = null
} }
@ -848,7 +851,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
viewModel.nowPlayingNotification?.destroySelf() viewModel.nowPlayingNotification?.destroySelf()
viewModel.nowPlayingNotification = null viewModel.nowPlayingNotification = null
if (!viewModel.shouldUseExistingPlayer) { if (!viewModel.isOrientationChangeInProgress) {
exoPlayer.stop() exoPlayer.stop()
exoPlayer.release() exoPlayer.release()
} }
@ -931,7 +934,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
binding.sbSkipBtn.isGone = true binding.sbSkipBtn.isGone = true
// set media sources for the player // set media sources for the player
if (!viewModel.shouldUseExistingPlayer) initStreamSources() if (!viewModel.isOrientationChangeInProgress) initStreamSources()
if (PreferenceHelper.getBoolean(PreferenceKeys.AUTO_FULLSCREEN_SHORTS, false) && if (PreferenceHelper.getBoolean(PreferenceKeys.AUTO_FULLSCREEN_SHORTS, false) &&
isShort && binding.playerMotionLayout.progress == 0f isShort && binding.playerMotionLayout.progress == 0f
@ -990,7 +993,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
exoPlayer.setPlaybackSpeed(1f) exoPlayer.setPlaybackSpeed(1f)
} }
viewModel.shouldUseExistingPlayer = false viewModel.isOrientationChangeInProgress = false
} }
} }
@ -1670,7 +1673,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
} }
playerLayoutOrientation = orientation playerLayoutOrientation = orientation
viewModel.shouldUseExistingPlayer = true viewModel.isOrientationChangeInProgress = true
activity?.recreate() activity?.recreate()
} }
} }

View File

@ -37,10 +37,11 @@ class PlayerViewModel : ViewModel() {
var sponsorBlockConfig = PlayerHelper.getSponsorBlockCategories() 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 * 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 isMiniPlayerVisible = MutableLiveData(false)
val isFullscreen = MutableLiveData(false) val isFullscreen = MutableLiveData(false)
@ -56,7 +57,7 @@ class PlayerViewModel : ViewModel() {
*/ */
suspend fun fetchVideoInfo(context: Context, videoId: String): Pair<Streams?, String?> = suspend fun fetchVideoInfo(context: Context, videoId: String): Pair<Streams?, String?> =
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
if (shouldUseExistingPlayer && streamsInfo != null) return@withContext streamsInfo to null if (isOrientationChangeInProgress && streamsInfo != null) return@withContext streamsInfo to null
streamsInfo = try { streamsInfo = try {
RetrofitInstance.api.getStreams(videoId).apply { RetrofitInstance.api.getStreams(videoId).apply {
@ -75,7 +76,7 @@ class PlayerViewModel : ViewModel() {
} }
suspend fun fetchSponsorBlockSegments(videoId: String) = withContext(Dispatchers.IO) { suspend fun fetchSponsorBlockSegments(videoId: String) = withContext(Dispatchers.IO) {
if (sponsorBlockConfig.isEmpty() || shouldUseExistingPlayer) return@withContext if (sponsorBlockConfig.isEmpty() || isOrientationChangeInProgress) return@withContext
runCatching { runCatching {
segments = segments =
@ -88,7 +89,7 @@ class PlayerViewModel : ViewModel() {
@OptIn(UnstableApi::class) @OptIn(UnstableApi::class)
fun keepOrCreatePlayer(context: Context): Pair<ExoPlayer, DefaultTrackSelector> { fun keepOrCreatePlayer(context: Context): Pair<ExoPlayer, DefaultTrackSelector> {
if (!shouldUseExistingPlayer || player == null || trackSelector == null) { if (!isOrientationChangeInProgress || player == null || trackSelector == null) {
this.trackSelector = DefaultTrackSelector(context) this.trackSelector = DefaultTrackSelector(context)
this.player = PlayerHelper.createPlayer(context, trackSelector!!, false) this.player = PlayerHelper.createPlayer(context, trackSelector!!, false)
} }