Skip to content

Instantly share code, notes, and snippets.

@halilozercan
Last active March 22, 2021 11:54
Show Gist options
  • Select an option

  • Save halilozercan/f85a85761cd5b24874fb68b6572a1e8c to your computer and use it in GitHub Desktop.

Select an option

Save halilozercan/f85a85761cd5b24874fb68b6572a1e8c to your computer and use it in GitHub Desktop.
@Composable
fun VideoPlayer() {
// This is the official way to access current context from Composable functions
val context = ContextAmbient.current
// Do not recreate the player everytime this Composable commits
val exoPlayer = remember {
SimpleExoPlayer.Builder(context).build().apply {
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
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
))
this.prepare(source)
}
}
// Gateway to traditional Android Views
AndroidView(
viewBlock = { context ->
PlayerView(context).apply {
player = exoPlayer
playWhenReady = true
}
}
)
}
@ppareit
Copy link

ppareit commented Mar 21, 2021

I had to change viewBlock to factory and playWhenReady is not a member. But it could be my version of the libraries (compose_version = 1.0.0-beta02). And I'm just starting to understand compose, so expect some misunderstandings.

@halilozercan
Copy link
Author

Yes you are right, this was written for beta01 and an older version of ExoPlayer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment