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
@@ -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()
) )
} }
@@ -22,8 +22,7 @@ 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(
@@ -32,23 +31,24 @@ import com.vitorpamplona.amethyst.service.HttpClient
exoDatabaseProvider exoDatabaseProvider
) )
cacheDataSourceFactory = CacheDataSource.Factory() renewCacheFactory()
.setCache(simpleCache) }
.setUpstreamDataSourceFactory(
OkHttpDataSource.Factory(HttpClient.getHttpClient()) // This method should be called when proxy setting changes.
) fun renewCacheFactory() {
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR) cacheDataSourceFactory = CacheDataSource.Factory()
} else { .setCache(simpleCache)
cacheDataSourceFactory = CacheDataSource.Factory() .setUpstreamDataSourceFactory(
.setCache(simpleCache) OkHttpDataSource.Factory(HttpClient.getHttpClient())
.setUpstreamDataSourceFactory( )
OkHttpDataSource.Factory(HttpClient.getHttpClient()) .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
) }
.setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
} fun get(context: Context): CacheDataSource.Factory {
if (!this::simpleCache.isInitialized) {
init(context)
} }
fun get(): CacheDataSource.Factory {
return cacheDataSourceFactory return cacheDataSourceFactory
} }
} }