mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-28 21:53:01 +02:00
Caching videos to reduce initialization delay.
This commit is contained in:
41
app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt
Normal file
41
app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package com.vitorpamplona.amethyst
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.google.android.exoplayer2.database.StandaloneDatabaseProvider
|
||||||
|
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource
|
||||||
|
import com.google.android.exoplayer2.upstream.cache.CacheDataSource
|
||||||
|
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor
|
||||||
|
import com.google.android.exoplayer2.upstream.cache.SimpleCache
|
||||||
|
|
||||||
|
object VideoCache {
|
||||||
|
|
||||||
|
var exoPlayerCacheSize: Long = 90 * 1024 * 1024 // 90MB
|
||||||
|
|
||||||
|
var leastRecentlyUsedCacheEvictor = LeastRecentlyUsedCacheEvictor(exoPlayerCacheSize)
|
||||||
|
|
||||||
|
lateinit var exoDatabaseProvider: StandaloneDatabaseProvider
|
||||||
|
lateinit var simpleCache: SimpleCache
|
||||||
|
|
||||||
|
lateinit var cacheDataSourceFactory: CacheDataSource.Factory
|
||||||
|
|
||||||
|
fun get(context: Context): CacheDataSource.Factory {
|
||||||
|
if (!this::simpleCache.isInitialized) {
|
||||||
|
exoDatabaseProvider = StandaloneDatabaseProvider(context)
|
||||||
|
|
||||||
|
simpleCache = SimpleCache(
|
||||||
|
context.cacheDir,
|
||||||
|
leastRecentlyUsedCacheEvictor,
|
||||||
|
exoDatabaseProvider
|
||||||
|
)
|
||||||
|
|
||||||
|
cacheDataSourceFactory = CacheDataSource.Factory()
|
||||||
|
.setCache(simpleCache)
|
||||||
|
.setUpstreamDataSourceFactory(
|
||||||
|
DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(true)
|
||||||
|
)
|
||||||
|
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cacheDataSourceFactory
|
||||||
|
}
|
||||||
|
}
|
@@ -13,8 +13,12 @@ import com.google.android.exoplayer2.C
|
|||||||
import com.google.android.exoplayer2.ExoPlayer
|
import com.google.android.exoplayer2.ExoPlayer
|
||||||
import com.google.android.exoplayer2.MediaItem
|
import com.google.android.exoplayer2.MediaItem
|
||||||
import com.google.android.exoplayer2.Player
|
import com.google.android.exoplayer2.Player
|
||||||
|
import com.google.android.exoplayer2.source.ProgressiveMediaSource
|
||||||
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
|
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout
|
||||||
import com.google.android.exoplayer2.ui.StyledPlayerView
|
import com.google.android.exoplayer2.ui.StyledPlayerView
|
||||||
|
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource
|
||||||
|
import com.google.android.exoplayer2.upstream.cache.CacheDataSource
|
||||||
|
import com.vitorpamplona.amethyst.VideoCache
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun VideoView(videoUri: String) {
|
fun VideoView(videoUri: String) {
|
||||||
@@ -28,17 +32,17 @@ fun VideoView(videoUri: String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DisposableEffect(exoPlayer) {
|
DisposableEffect(exoPlayer) {
|
||||||
exoPlayer.setMediaItem(MediaItem.fromUri(videoUri))
|
exoPlayer.setMediaSource(
|
||||||
|
ProgressiveMediaSource.Factory(VideoCache.get(context.applicationContext)).createMediaSource(MediaItem.fromUri(videoUri))
|
||||||
|
)
|
||||||
exoPlayer.prepare()
|
exoPlayer.prepare()
|
||||||
|
|
||||||
onDispose {
|
onDispose {
|
||||||
exoPlayer.release()
|
exoPlayer.release()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AndroidView(
|
AndroidView(
|
||||||
modifier = Modifier
|
modifier = Modifier.fillMaxWidth(),
|
||||||
.fillMaxWidth(),
|
|
||||||
factory = {
|
factory = {
|
||||||
StyledPlayerView(context).apply {
|
StyledPlayerView(context).apply {
|
||||||
player = exoPlayer
|
player = exoPlayer
|
||||||
|
Reference in New Issue
Block a user