Moves SimpleCache creation to the IO thread.

This commit is contained in:
Vitor Pamplona
2024-06-03 17:17:26 -04:00
parent 571b1bc9cf
commit 5e0a1f9de1
2 changed files with 15 additions and 9 deletions
@@ -40,6 +40,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.File import java.io.File
import kotlin.time.measureTimedValue import kotlin.time.measureTimedValue
@@ -53,7 +54,9 @@ class Amethyst : Application() {
val videoCache: VideoCache by lazy { val videoCache: VideoCache by lazy {
val newCache = VideoCache() val newCache = VideoCache()
newCache.initFileCache(this) runBlocking {
newCache.initFileCache(this@Amethyst)
}
newCache newCache
} }
@@ -27,6 +27,8 @@ import androidx.media3.datasource.cache.CacheDataSource
import androidx.media3.datasource.cache.LeastRecentlyUsedCacheEvictor import androidx.media3.datasource.cache.LeastRecentlyUsedCacheEvictor
import androidx.media3.datasource.cache.SimpleCache import androidx.media3.datasource.cache.SimpleCache
import androidx.media3.datasource.okhttp.OkHttpDataSource import androidx.media3.datasource.okhttp.OkHttpDataSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import java.io.File import java.io.File
@@ -41,16 +43,17 @@ class VideoCache {
lateinit var cacheDataSourceFactory: CacheDataSource.Factory lateinit var cacheDataSourceFactory: CacheDataSource.Factory
@Synchronized suspend fun initFileCache(context: Context) {
fun initFileCache(context: Context) {
exoDatabaseProvider = StandaloneDatabaseProvider(context) exoDatabaseProvider = StandaloneDatabaseProvider(context)
simpleCache = withContext(Dispatchers.IO) {
SimpleCache( simpleCache =
File(context.cacheDir, "exoplayer"), SimpleCache(
leastRecentlyUsedCacheEvictor, File(context.cacheDir, "exoplayer"),
exoDatabaseProvider, leastRecentlyUsedCacheEvictor,
) exoDatabaseProvider,
)
}
} }
// This method should be called when proxy setting changes. // This method should be called when proxy setting changes.