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

View File

@@ -22,33 +22,33 @@ import com.vitorpamplona.amethyst.service.HttpClient
@Synchronized
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
fun init(context: Context) {
if (!this::simpleCache.isInitialized) {
exoDatabaseProvider = StandaloneDatabaseProvider(context)
private fun init(context: Context) {
exoDatabaseProvider = StandaloneDatabaseProvider(context)
simpleCache = SimpleCache(
context.cacheDir,
leastRecentlyUsedCacheEvictor,
exoDatabaseProvider
)
simpleCache = SimpleCache(
context.cacheDir,
leastRecentlyUsedCacheEvictor,
exoDatabaseProvider
)
cacheDataSourceFactory = CacheDataSource.Factory()
.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)
}
renewCacheFactory()
}
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
}
}