Last active
March 4, 2021 07:04
-
-
Save halilozercan/dd56c9168b4f3e01d9126a29a9be504b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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