A simple adaptation to Jetpack compose of this answer
@Composable
fun VideoPlayer(myVideo: MyVideo, modifier: Modifier = Modifier) {
val context = LocalContext.current
val exoPlayer = remember {
ExoPlayer.Builder(context).build().apply {
setMediaItem(myVideo.asMediaItem())
repeatMode = ExoPlayer.REPEAT_MODE_ALL
playWhenReady = true
prepare()
play()
}
}
DisposableEffect(
AndroidView(
modifier = modifier,
factory = {
PlayerView(context).apply {
player = exoPlayer
useController = false
FrameLayout.LayoutParams(
ViewGroup.LayoutParams
.MATCH_PARENT,
ViewGroup.LayoutParams
.MATCH_PARENT
)
}
}
)
) {
onDispose {
exoPlayer.release()
}
}
}
This would be asMediaItem()
extension function
private fun MyVideo.asMediaItem() =
MediaItem.Builder()
.setUri(url)
.setMediaId("some-media-id")
.setTag("some-video-tag")
.setMediaMetadata(
MediaMetadata.Builder().setDisplayTitle("Video").build()
)
.build()
and MyVideo class, which models a really basic video
data class MyVideo(val url: String, val previewImage: String)
The key line to remove the controls here is useController = false