Skip to content

Instantly share code, notes, and snippets.

@halilozercan
Last active March 4, 2021 07:04
Show Gist options
  • Save halilozercan/dd56c9168b4f3e01d9126a29a9be504b to your computer and use it in GitHub Desktop.
Save halilozercan/dd56c9168b4f3e01d9126a29a9be504b to your computer and use it in GitHub Desktop.
@Composable
fun VideoPlayer(sourceUrl: String) {
// This is the official way to access current context from Composable functions
val context = LocalContext.current
// Do not recreate the player everytime this Composable commits
val exoPlayer = remember {
SimpleExoPlayer.Builder(context).build()
}
// We only want to react to changes in sourceUrl.
// This effect will be executed at each commit phase if
// [sourceUrl] has changed.
LaunchedEffect(sourceUrl) {
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(context,
Util.getUserAgent(context, context.packageName))
val source = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(
// Big Buck Bunny from Blender Project
sourceUrl
))
exoPlayer.prepare(source)
}
// Gateway to traditional Android Views
AndroidView(
viewBlock = { context ->
PlayerView(context).apply {
player = exoPlayer
playWhenReady = true
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment