Skip to content

Instantly share code, notes, and snippets.

@rubenquadros
Last active August 7, 2022 11:36
Show Gist options
  • Save rubenquadros/23efb692b919a6635dc9bf760e311e8b to your computer and use it in GitHub Desktop.
Save rubenquadros/23efb692b919a6635dc9bf760e311e8b to your computer and use it in GitHub Desktop.
Action to be performed by the bottom controls
@Composable
fun BottomControls(
modifier: Modifier = Modifier,
totalDuration: () -> Long,
currentTime: () -> Long,
bufferPercentage: () -> Int,
onSeekChanged: (timeMs: Float) -> Unit
) {
val duration = remember(totalDuration()) { totalDuration() }
val videoTime = remember(currentTime()) { currentTime() }
val buffer = remember(bufferPercentage()) { bufferPercentage() }
Column(modifier = modifier.padding(bottom = 32.dp)) {
Box(modifier = Modifier.fillMaxWidth()) {
// buffer bar
Slider(
value = buffer.toFloat(),
enabled = false,
onValueChange = { /*do nothing*/},
valueRange = 0f..100f,
colors =
SliderDefaults.colors(
disabledThumbColor = Color.Transparent,
disabledActiveTrackColor = Color.Gray
)
)
// seek bar
Slider(
modifier = Modifier.fillMaxWidth(),
value = videoTime.toFloat(),
onValueChange = onSeekChanged,
valueRange = 0f..duration.toFloat(),
colors =
SliderDefaults.colors(
thumbColor = Purple200,
activeTickColor = Purple200
)
)
}
Row(
modifier = Modifier.fillMaxWidth().padding(top = 16.dp),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
modifier = Modifier.padding(horizontal = 16.dp),
text = duration.formatMinSec(),
color = Purple200
)
IconButton(
modifier = Modifier.padding(horizontal = 16.dp),
onClick = {}
) {
Image(
contentScale = ContentScale.Crop,
painter = painterResource(id = R.drawable.ic_fullscreen),
contentDescription = "Enter/Exit fullscreen"
)
}
}
}
}
//////////////////////////////////////////////
// Call site
//////////////////////////////////////////////
PlayerControls(
modifier = Modifier.fillMaxSize(),
isPlaying = { isPlaying },
onReplayClick = { exoPlayer.seekBack() },
onForwardClick = { exoPlayer.seekForward() },
onPauseToggle = {
if (exoPlayer.isPlaying) {
exoPlayer.pause()
} else {
exoPlayer.play()
}
isPlaying = isPlaying.not()
},
totalDuration = { totalDuration },
currentTime = { currentTime },
bufferPercentage = { bufferPercentage },
onSeekChanged = { timeMs: Float -> exoPlayer.seekTo(timeMs.toLong()) }
)
//////////////////////////////////////////////
// Helper function
//////////////////////////////////////////////
fun Long.formatMinSec(): String {
return if (this == 0L) {
"..."
} else {
String.format(
"%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(this),
TimeUnit.MILLISECONDS.toSeconds(this) -
TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(this)
)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment