Skip to content

Instantly share code, notes, and snippets.

@rubenquadros
Created September 4, 2021 11:08
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 rubenquadros/f663e0554bfa03f4b11a516326f3178e to your computer and use it in GitHub Desktop.
Save rubenquadros/f663e0554bfa03f4b11a516326f3178e to your computer and use it in GitHub Desktop.
Initial video player
@Composable
fun VideoPlayer(modifier: Modifier = Modifier) {
val context = LocalContext.current
// create our player
val exoPlayer = remember {
SimpleExoPlayer.Builder(context).build().apply {
this.prepare()
}
}
ConstraintLayout(modifier = modifier) {
val (title, videoPlayer) = createRefs()
// video title
Text(
text = "Current Title",
color = Color.White,
modifier =
Modifier.padding(16.dp)
.fillMaxWidth()
.wrapContentHeight()
.constrainAs(title) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
}
)
// player view
DisposableEffect(
AndroidView(
modifier =
Modifier.testTag("VideoPlayer")
.constrainAs(videoPlayer) {
top.linkTo(parent.top)
start.linkTo(parent.start)
end.linkTo(parent.end)
bottom.linkTo(parent.bottom)
},
factory = {
// exo player view for our video player
PlayerView(context).apply {
player = exoPlayer
layoutParams =
FrameLayout.LayoutParams(
ViewGroup.LayoutParams
.MATCH_PARENT,
ViewGroup.LayoutParams
.MATCH_PARENT
)
}
}
)
) {
onDispose {
// relase player when no longer needed
exoPlayer.release()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment