Merges VideoCache initialization with get methods

This commit is contained in:
Vitor Pamplona
2023-07-21 10:08:04 -04:00
parent 6723225885
commit 8dd5705f02
2 changed files with 25 additions and 30 deletions

View File

@@ -16,7 +16,7 @@ class PlaybackService : MediaSessionService() {
MultiPlayerPlaybackManager(HlsMediaSource.Factory(OkHttpDataSource.Factory(HttpClient.getHttpClient()))) MultiPlayerPlaybackManager(HlsMediaSource.Factory(OkHttpDataSource.Factory(HttpClient.getHttpClient())))
} }
private val managerProgressive by lazy { private val managerProgressive by lazy {
MultiPlayerPlaybackManager(ProgressiveMediaSource.Factory(VideoCache.get())) MultiPlayerPlaybackManager(ProgressiveMediaSource.Factory(VideoCache.get(applicationContext)))
} }
private val managerLocal by lazy { private val managerLocal by lazy {
MultiPlayerPlaybackManager() MultiPlayerPlaybackManager()
@@ -27,13 +27,8 @@ class PlaybackService : MediaSessionService() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
// Initializes video cache.
VideoCache.init(applicationContext)
setMediaNotificationProvider( setMediaNotificationProvider(
DefaultMediaNotificationProvider.Builder(applicationContext) DefaultMediaNotificationProvider.Builder(applicationContext).build()
// .setNotificationIdProvider { session -> session.id.hashCode() }
.build()
) )
} }

View File

@@ -22,33 +22,33 @@ import com.vitorpamplona.amethyst.service.HttpClient
@Synchronized @Synchronized
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
fun init(context: Context) { private fun init(context: Context) {
if (!this::simpleCache.isInitialized) { exoDatabaseProvider = StandaloneDatabaseProvider(context)
exoDatabaseProvider = StandaloneDatabaseProvider(context)
simpleCache = SimpleCache( simpleCache = SimpleCache(
context.cacheDir, context.cacheDir,
leastRecentlyUsedCacheEvictor, leastRecentlyUsedCacheEvictor,
exoDatabaseProvider exoDatabaseProvider
) )
cacheDataSourceFactory = CacheDataSource.Factory() renewCacheFactory()
.setCache(simpleCache)
.setUpstreamDataSourceFactory(
OkHttpDataSource.Factory(HttpClient.getHttpClient())
)
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
} else {
cacheDataSourceFactory = CacheDataSource.Factory()
.setCache(simpleCache)
.setUpstreamDataSourceFactory(
OkHttpDataSource.Factory(HttpClient.getHttpClient())
)
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
}
} }
fun get(): CacheDataSource.Factory { // This method should be called when proxy setting changes.
fun renewCacheFactory() {
cacheDataSourceFactory = CacheDataSource.Factory()
.setCache(simpleCache)
.setUpstreamDataSourceFactory(
OkHttpDataSource.Factory(HttpClient.getHttpClient())
)
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
}
fun get(context: Context): CacheDataSource.Factory {
if (!this::simpleCache.isInitialized) {
init(context)
}
return cacheDataSourceFactory return cacheDataSourceFactory
} }
} }