Skip to content

Instantly share code, notes, and snippets.

@AndroBrain
Last active September 2, 2024 04:49
Show Gist options
  • Save AndroBrain/eef94f4ab3d12e01b73f652f2ffa625c to your computer and use it in GitHub Desktop.
Save AndroBrain/eef94f4ab3d12e01b73f652f2ffa625c to your computer and use it in GitHub Desktop.
VideoSurface
private const val VIDEO_URL = "https://cdn.pixabay.com/video/2015/08/20/468-136808389_large.mp4"
@Composable
fun PlayerScreen() {
val context = LocalContext.current
val exoPlayer = remember {
ExoPlayer.Builder(context).build().apply {
// Configure the player
// here I'm making the video loop
repeatMode = ExoPlayer.REPEAT_MODE_ALL
playWhenReady = true
setMediaItem(MediaItem.fromUri(VIDEO_URL))
prepare()
}
}
VideoSurface(modifier = Modifier.fillMaxSize(), exoPlayer = exoPlayer)
}
@Composable
fun VideoSurface(modifier: Modifier = Modifier, exoPlayer: ExoPlayer) {
AndroidExternalSurface(
modifier = modifier,
onInit = {
onSurface { surface, _, _ ->
exoPlayer.setVideoSurface(surface)
surface.onDestroyed { exoPlayer.setVideoSurface(null) }
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment