Daniele Segato
08/25/2021, 9:11 AMFilip Wiesner
08/25/2021, 9:14 AMDaniele Segato
08/25/2021, 9:16 AMexoPlayer.release()
in both cases (I still have to save the state and resume it)
(Also can anyone tell me if you see any problem in the code below?)
@Composable
fun Video() {
val context = LocalContext.current
val lifecycleState by LocalLifecycleOwner.current.lifecycle.collectState()
val exoPlayer = remember {
SimpleExoPlayer.Builder(context.applicationContext)
.build()
}
DisposableEffect(key1 = exoPlayer) {
val videoItem =
MediaItem.fromUri("<https://storage.googleapis.com/exoplayer-test-media-0/BigBuckBunny_320x180.mp4>")
exoPlayer.setMediaItem(videoItem)
// exoPlayer.volume = 0f
exoPlayer.prepare()
onDispose { exoPlayer.release() }
}
exoPlayer.playWhenReady = lifecycleState.isAtLeast(Lifecycle.State.RESUMED)
AndroidView(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(16f / 9f),
factory = { context ->
PlayerView(context).apply {
player = exoPlayer
useController = false
}
}
)
}
@Composable
private fun Lifecycle.collectState(): State<Lifecycle.State> {
val state = remember { mutableStateOf(currentState) }
DisposableEffect(this) {
val listener = LifecycleEventObserver { _, _ ->
state.value = currentState
}
addObserver(listener)
onDispose {
removeObserver(listener)
}
}
return state
}
I've found this https://github.com/evant/compose-shown/blob/main/shown/src/main/java/me/tatarka/compose/shown/Shown.kt
containing some code to detect when a Composable becomes visible (I also need the opposite) is there any better way / API to monitor how much of a composable is visible on the screen at any given time?
ThanksFilip Wiesner
08/25/2021, 9:17 AMChris Miller
08/25/2021, 9:30 AMDaniele Segato
08/25/2021, 9:33 AMChris Miller
08/25/2021, 9:35 AMval state = rememberLazyListState()
LazyColumn(state = state, ...) {
...
}
if (state.isVisible(videoItemIndex) {
// start video if not already started
} else {
// stop video if not already stopped
}
You'll likely need extra smarts around the state handling, but the above is the general gist of it I thinkDaniele Segato
08/25/2021, 9:44 AMisVisible()
method in LazyListStateChris Miller
08/25/2021, 9:51 AMprivate fun LazyListState.isVisible(index: Int): Boolean {
return index >= firstVisibleItemIndex && index < firstVisibleItemIndex + layoutInfo.visibleItemsInfo.size
}
Daniele Segato
08/25/2021, 9:54 AMval isVisible: State<Boolean>
and as far as I can nor LazyListState.layoutInfo
nor firstVisibleItemIndex give me a state I can build uponAlbert Chang
08/25/2021, 9:58 AMsnapshotFlow { listState.layoutInfo.visibleItemsInfo.any { it.key == someKey } }.collect()
Daniele Segato
08/25/2021, 10:03 AMprivate val layoutInfoState = mutableStateOf<LazyListLayoutInfo>(EmptyLazyListLayoutInfo)
would it work anyway?Albert Chang
08/25/2021, 10:07 AMlayoutInfo
is a property backed by a mutable state, not a variable.
And you don't necessarily need a state. Flow is also observable. You can collect the snapshot flow in a LaunchedEffect
and when the item becomes invisible you stop the player.Daniele Segato
08/25/2021, 10:14 AM