I have a local HTTP server where I'm hosting an MP4 file (about 50MB). I set up the following Composable in my app:
@Composable
private fun VideoItem(
url: String,
modifier: Modifier = Modifier
) {
val context = LocalContext.current
val player = remember(context) {
ExoPlayer.Builder(context)
.build()
.apply {
val mediaItem = MediaItem.fromUri(url)
setMediaItem(mediaItem)
prepare()
play()
}
}
DisposableEffect(Unit) {
onDispose {
player.release()
}
}
Box(
modifier = modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
AndroidView(
factory = { context ->
PlayerView(context).apply {
this.player = player
}
}
)
}
}
The URL is something like "http://192.168.123.123:8080/sample.mp4", and I make sure that the phone is connected to the same network when accessing it.
However, it takes around 50 seconds for the initial prepare()
to finish - that is, the video takes about 50 seconds to load before it even shows the total time and starts playing. That raises a few questions for me:
- Is it possible that ExoPlayer attempts to download the entire file before it even begins to play it? That would explain the delay of 50 seconds for 50MB, as 1 MB/s seems about right for the network.
- If 1) is true, how come the full video is not loaded immediately? That is, if I skip to some further point in the video, the "buffering" still appears and it takes a few seconds to start playing from that point.
- Is there a way to speed up this initial loading? Or somehow stream the video instead?
I am using Compose BOM 2023.06.01 and Media3 1.1.1.