Merge pull request #2925 from vitorpamplona/claude/fix-background-video-playback-udxXX
fix(video): pause playback when app goes to background
This commit is contained in:
+41
@@ -26,6 +26,9 @@ import androidx.compose.runtime.DisposableEffect
|
|||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.MutableState
|
import androidx.compose.runtime.MutableState
|
||||||
import androidx.compose.ui.platform.LocalView
|
import androidx.compose.ui.platform.LocalView
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
|
import androidx.lifecycle.LifecycleEventObserver
|
||||||
|
import androidx.lifecycle.compose.LocalLifecycleOwner
|
||||||
import androidx.media3.common.Player
|
import androidx.media3.common.Player
|
||||||
import com.vitorpamplona.amethyst.service.playback.pip.BackgroundMedia
|
import com.vitorpamplona.amethyst.service.playback.pip.BackgroundMedia
|
||||||
|
|
||||||
@@ -75,6 +78,44 @@ fun ControlWhenPlayerIsActive(
|
|||||||
listener.destroy()
|
listener.destroy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pause when the host activity leaves the foreground; resume the visible
|
||||||
|
// video when it comes back. The scroll-based mutex above only reacts to
|
||||||
|
// on-screen position, which doesn't change on background — so without
|
||||||
|
// this, videos play forever behind another app.
|
||||||
|
// Skip the explicit BackgroundMedia (PiP) instance: the user opted that
|
||||||
|
// one into keep-playing.
|
||||||
|
val lifecycleOwner = LocalLifecycleOwner.current
|
||||||
|
DisposableEffect(lifecycleOwner, mediaControllerState) {
|
||||||
|
val observer =
|
||||||
|
LifecycleEventObserver { _, event ->
|
||||||
|
when (event) {
|
||||||
|
Lifecycle.Event.ON_PAUSE -> {
|
||||||
|
if (controller.isPlaying && !BackgroundMedia.isMutex(mediaControllerState)) {
|
||||||
|
controller.pause()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Lifecycle.Event.ON_RESUME -> {
|
||||||
|
if (automaticallyStartPlayback &&
|
||||||
|
isClosestToTheCenterOfTheScreen.value &&
|
||||||
|
!controller.isPlaying
|
||||||
|
) {
|
||||||
|
if (BackgroundMedia.hasBackgroundButNot(mediaControllerState)) {
|
||||||
|
controller.volume = 0f
|
||||||
|
}
|
||||||
|
controller.play()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
Unit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lifecycleOwner.lifecycle.addObserver(observer)
|
||||||
|
onDispose { lifecycleOwner.lifecycle.removeObserver(observer) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PlayerEventListener(
|
class PlayerEventListener(
|
||||||
|
|||||||
Reference in New Issue
Block a user