Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: playback pauses after orientation change #5750

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -848,7 +851,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
viewModel.nowPlayingNotification?.destroySelf()
viewModel.nowPlayingNotification = null

if (!viewModel.shouldUseExistingPlayer) {
if (!viewModel.isOrientationChangeInProgress) {
exoPlayer.stop()
exoPlayer.release()
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -990,7 +993,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
exoPlayer.setPlaybackSpeed(1f)
}

viewModel.shouldUseExistingPlayer = false
viewModel.isOrientationChangeInProgress = false
}
}

Expand Down Expand Up @@ -1670,7 +1673,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
}
playerLayoutOrientation = orientation

viewModel.shouldUseExistingPlayer = true
viewModel.isOrientationChangeInProgress = true
activity?.recreate()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -56,7 +57,7 @@ class PlayerViewModel : ViewModel() {
*/
suspend fun fetchVideoInfo(context: Context, videoId: String): Pair<Streams?, String?> =
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 {
Expand All @@ -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 =
Expand All @@ -88,7 +89,7 @@ class PlayerViewModel : ViewModel() {

@OptIn(UnstableApi::class)
fun keepOrCreatePlayer(context: Context): Pair<ExoPlayer, DefaultTrackSelector> {
if (!shouldUseExistingPlayer || player == null || trackSelector == null) {
if (!isOrientationChangeInProgress || player == null || trackSelector == null) {
this.trackSelector = DefaultTrackSelector(context)
this.player = PlayerHelper.createPlayer(context, trackSelector!!, false)
}
Expand Down
Loading