Fix ExoPlayer unpausing when returning to foreground

The lifecycle observer was holding onto a stale playWhenReady value,
so every time you came back to the app it would resume playback even
if you had explicitly paused the video. Now it uses rememberUpdatedState
to check the live value instead.
This commit is contained in:
VenusIsJaded 2026-05-16 01:12:06 -05:00
parent 70d3eee9d2
commit e8d74d9194

View file

@ -81,7 +81,9 @@ actual fun PlatformPlayerSurface(
val lifecycleOwner = LocalLifecycleOwner.current val lifecycleOwner = LocalLifecycleOwner.current
val latestOnSnapshot = rememberUpdatedState(onSnapshot) val latestOnSnapshot = rememberUpdatedState(onSnapshot)
val latestOnError = rememberUpdatedState(onError) val latestOnError = rememberUpdatedState(onError)
val latestPlayWhenReady = rememberUpdatedState(playWhenReady)
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val latestPlayWhenReady = rememberUpdatedState(playWhenReady)
val playerSettings = remember { val playerSettings = remember {
PlayerSettingsRepository.ensureLoaded() PlayerSettingsRepository.ensureLoaded()
@ -251,7 +253,7 @@ actual fun PlatformPlayerSurface(
val activity = context.findActivity() val activity = context.findActivity()
val observer = LifecycleEventObserver { _, event -> val observer = LifecycleEventObserver { _, event ->
when (event) { when (event) {
Lifecycle.Event.ON_START -> exoPlayer.playWhenReady = playWhenReady Lifecycle.Event.ON_START -> exoPlayer.playWhenReady = latestPlayWhenReady.value
Lifecycle.Event.ON_STOP -> { Lifecycle.Event.ON_STOP -> {
val isInPictureInPicture = val isInPictureInPicture =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity?.isInPictureInPictureMode == true Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity?.isInPictureInPictureMode == true
@ -271,7 +273,7 @@ actual fun PlatformPlayerSurface(
} }
LaunchedEffect(exoPlayer, playWhenReady) { LaunchedEffect(exoPlayer, playWhenReady) {
exoPlayer.playWhenReady = playWhenReady exoPlayer.playWhenReady = latestPlayWhenReady.value
syncPlayerViewKeepScreenOn() syncPlayerViewKeepScreenOn()
latestOnSnapshot.value(exoPlayer.snapshot()) latestOnSnapshot.value(exoPlayer.snapshot())
} }