player: Pause and mute out when the app goes to the background
Pressing HOME kept the audio running, so a video started in another app played on top of it and returning landed on the home view with an invisible player. The player now pauses itself on ON_PAUSE, reports the position to the server and resumes automatically when the app comes back. The playing screen is also kept across activity recreation now, so returning shows the video, not the home view. Ref #6
This commit is contained in:
parent
a5d88aeea1
commit
ed65b77e90
2 changed files with 28 additions and 1 deletions
|
|
@ -78,7 +78,7 @@ fun HomeScreen(viewModel: AppViewModel = androidx.lifecycle.viewmodel.compose.vi
|
|||
var destination by rememberSaveable { mutableStateOf(Destination.Home) }
|
||||
var channelDetails by rememberSaveable { mutableStateOf<String?>(null) } // handle
|
||||
// NOT saveable: launching the app should never resume a half-watched player session.
|
||||
var playingVideo by remember { mutableStateOf<String?>(null) } // shortUUID
|
||||
var playingVideo by rememberSaveable { mutableStateOf<String?>(null) } // shortUUID
|
||||
var restoreToken by rememberSaveable { mutableStateOf(0) } // increments when playback ends
|
||||
var drawerOpen by rememberSaveable { mutableStateOf(false) }
|
||||
val context = androidx.compose.ui.platform.LocalContext.current
|
||||
|
|
|
|||
|
|
@ -270,6 +270,33 @@ private fun PlayerSurface(
|
|||
|
||||
LaunchedEffect(Unit) { runCatching { keyFocus.requestFocus() } }
|
||||
|
||||
// Leaving the app (HOME key, task switch) must not keep the sound going:
|
||||
// pause on ON_PAUSE, report the position, and resume on return (issue #6).
|
||||
var autoPaused by remember { mutableStateOf(false) }
|
||||
val lifecycleOwner = androidx.lifecycle.compose.LocalLifecycleOwner.current
|
||||
DisposableEffect(lifecycleOwner) {
|
||||
val observer = androidx.lifecycle.LifecycleEventObserver { _, event ->
|
||||
when (event) {
|
||||
androidx.lifecycle.Lifecycle.Event.ON_PAUSE -> {
|
||||
if (player.isPlaying) {
|
||||
autoPaused = true
|
||||
player.pause()
|
||||
}
|
||||
viewModel.persistExit(shortUuid, (player.currentPosition / 1000).toInt())
|
||||
}
|
||||
androidx.lifecycle.Lifecycle.Event.ON_RESUME -> {
|
||||
if (autoPaused) {
|
||||
autoPaused = false
|
||||
player.play()
|
||||
}
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
lifecycleOwner.lifecycle.addObserver(observer)
|
||||
onDispose { lifecycleOwner.lifecycle.removeObserver(observer) }
|
||||
}
|
||||
|
||||
DisposableEffect(s.playUrl) {
|
||||
val item = MediaItem.Builder()
|
||||
.setUri(s.playUrl)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue