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 when enter background video not pause (because currently the life… #173

Merged
Merged
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 @@ -126,40 +126,46 @@ fun VideoPlayer(
customControl.player = player
}
}

var isResume by remember {
mutableStateOf(true)
}
DisposableEffect(Unit) {
val observer = object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
isResume = true
player.playWhenReady = autoPlay
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onPause() {
isResume = false
updateState()
player.playWhenReady = false
}
}
lifecycle.addObserver(observer)
onDispose {
updateState()
player.release()
lifecycle.removeObserver(observer)
}
}

AndroidView(
modifier = modifier,
factory = { context ->
StyledPlayerView(context).also { playerView ->
playerView.videoSurfaceView?.let {
if (it is SurfaceView) it.setZOrderMediaOverlay(zOrderMediaOverlay)
}
(playerView.videoSurfaceView as? SurfaceView)?.setZOrderMediaOverlay(zOrderMediaOverlay)
playerView.useController = showControls
lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
playerView.onResume()
player.playWhenReady = autoPlay
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
updateState()
playerView.onPause()
player.playWhenReady = false
}
})
}
}
) {
it.player = player
if (isResume) {
it.onResume()
} else {
it.onPause()
}
}
}
if ((shouldShowThumb || !playing) && thumb != null) {
Expand Down