Mohammad Jahidul Islam
08/13/2021, 4:48 AMrajesh
08/13/2021, 11:34 AMMohammad Jahidul Islam
08/13/2021, 11:41 AMrajesh
08/13/2021, 12:07 PMclass VideoCache {
companion object {
private var cache: Cache? = null
fun getInstance(context: Context): Cache {
if (cache == null) {
cache = SimpleCache(
File(context.cacheDir, "media"),
LeastRecentlyUsedCacheEvictor((500 * 1024 * 1024).toLong()), //500MB of cache will be stored on device
ExoDatabaseProvider(context)
)
}
return cache as Cache
}
}
}
@Composable
fun VideoPlayer(url: String) {
val context = LocalContext.current
val exoPlayer = remember {
SimpleExoPlayer.Builder(context)
.build()
.apply {
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(
context,
Util.getUserAgent(context, context.packageName)
)
val cacheDataSourceFactory =
CacheDataSource.Factory().setCache(VideoCache.getInstance(context))
.setUpstreamDataSourceFactory(dataSourceFactory)
val source = ProgressiveMediaSource.Factory(
cacheDataSourceFactory
).createMediaSource(MediaItem.fromUri(Uri.parse(url)))
setMediaSource(source)
prepare()
}
}
exoPlayer.playWhenReady = true
exoPlayer.videoScalingMode = C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
exoPlayer.repeatMode = Player.REPEAT_MODE_ONE
DisposableEffect(
AndroidView(
factory = {
PlayerView(context).apply {
hideController()
useController = false
resizeMode = AspectRatioFrameLayout.RESIZE_MODE_ZOOM
player = exoPlayer
layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
}
},
modifier = modifier.clickable(onClick = {
exoPlayer.playWhenReady = !exoPlayer.playWhenReady
})
)
) {
onDispose {
exoPlayer.release()
}
}
}
rajesh
08/13/2021, 12:08 PM