https://kotlinlang.org logo
#compose
Title
# compose
k

Kshitij Patil

12/05/2020, 1:20 PM
I'm using
exoplayer
with compose and for some reason the video is not getting shown. The audio starts playing as soon as the composable is rendered but video is not working. I've tried the latest version and
2.11.7
version of exoplayer and I'm currently using compose
alpha07
Copy code
val context = ContextAmbient.current
val exoPlayer = remember(mediaUri) {
    SimpleExoPlayer.Builder(context)
        .build()
        .apply {
            val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(
                context,
                Util.getUserAgent(context, context.packageName)
            )
            val source = HlsMediaSource.Factory(dataSourceFactory)
                .createMediaSource(mediaUri.toUri())
            this.prepare(source)
        }
}

Box(
    modifier = Modifier.preferredSize(244.dp)
        .padding(vertical = 24.dp, horizontal = 8.dp)
        .clip(RoundedCornerShape(12.dp)),
) {
    AndroidView(
        viewBlock = { context ->
            PlayerView(context).apply {
                layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
                useController = false
                resizeMode = RESIZE_MODE_ZOOM
                player = exoPlayer
                exoPlayer.playWhenReady = true
            }
        },
        modifier = Modifier.fillMaxSize()
    )
}

onDispose {
    exoPlayer.stop()
    exoPlayer.release()
}
Here is my implementation snippet, any help would be appreciated. Thanks!
18 Views