Skip to content

Instantly share code, notes, and snippets.

@shkcodes
Created July 17, 2021 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shkcodes/2d845d71ab2df1e82524cd20aac2d49d to your computer and use it in GitHub Desktop.
Save shkcodes/2d845d71ab2df1e82524cd20aac2d49d to your computer and use it in GitHub Desktop.
val lifecycleOwner by rememberUpdatedState(LocalLifecycleOwner.current)
DisposableEffect(lifecycleOwner) {
val lifecycle = lifecycleOwner.lifecycle
val observer = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_PAUSE -> {
exoPlayer.playWhenReady = false
}
Lifecycle.Event.ON_RESUME -> {
exoPlayer.playWhenReady = true
}
Lifecycle.Event.ON_DESTROY -> {
exoPlayer.run {
stop()
release()
}
}
}
}
lifecycle.addObserver(observer)
onDispose {
lifecycle.removeObserver(observer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment