Skip to content

Instantly share code, notes, and snippets.

@rubenquadros
Last active July 31, 2022 12:26
Show Gist options
  • Save rubenquadros/c90d5960bdf541033b9eea9e0612d7d9 to your computer and use it in GitHub Desktop.
Save rubenquadros/c90d5960bdf541033b9eea9e0612d7d9 to your computer and use it in GitHub Desktop.
Action to be performed by the center controls
@Composable
fun CenterControls(
modifier: Modifier = Modifier,
isPlaying: () -> Boolean,
onReplayClick: () -> Unit,
onPauseToggle: () -> Unit,
onForwardClick: () -> Unit
) {
val isVideoPlaying = remember(isPlaying()) { isPlaying() }
Row(modifier = modifier, horizontalArrangement = Arrangement.SpaceEvenly) {
IconButton(modifier = Modifier.size(40.dp), onClick = onReplayClick) {
Image(
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop,
painter = painterResource(id = R.drawable.ic_replay_5),
contentDescription = "Replay 5 seconds"
)
}
IconButton(modifier = Modifier.size(40.dp), onClick = onPauseToggle) {
Image(
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop,
painter =
if (isVideoPlaying) {
painterResource(id = R.drawable.ic_pause)
} else {
painterResource(id = R.drawable.ic_play)
},
contentDescription = "Play/Pause"
)
}
IconButton(modifier = Modifier.size(40.dp), onClick = onForwardClick) {
Image(
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop,
painter = painterResource(id = R.drawable.ic_forward_10),
contentDescription = "Forward 10 seconds"
)
}
}
}
//////////////////////////////////////////////
// Call site
//////////////////////////////////////////////
var isPlaying by remember { mutableStateOf(true) }
PlayerControls(
modifier = Modifier.fillMaxSize(),
isPlaying = { isPlaying },
onReplayClick = { exoPlayer.seekBack() },
onForwardClick = { exoPlayer.seekForward() },
onPauseToggle = {
if (exoPlayer.isPlaying) {
// pause the video
exoPlayer.pause()
} else {
// play the video
// it's already paused
exoPlayer.play()
}
isPlaying = isPlaying.not()
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment