Created
September 4, 2021 11:08
-
-
Save rubenquadros/f663e0554bfa03f4b11a516326f3178e to your computer and use it in GitHub Desktop.
Initial video player
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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