diff --git a/app/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt b/app/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt index 21019a3c8..dcff97b6d 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt @@ -6,10 +6,13 @@ import android.os.StrictMode.ThreadPolicy import android.os.StrictMode.VmPolicy class Amethyst : Application() { + @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) override fun onCreate() { super.onCreate() instance = this + VideoCache.initFileCache(instance) + if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy( ThreadPolicy.Builder() diff --git a/app/src/main/java/com/vitorpamplona/amethyst/PlaybackService.kt b/app/src/main/java/com/vitorpamplona/amethyst/PlaybackService.kt index 670ecdc6c..01466e330 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/PlaybackService.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/PlaybackService.kt @@ -6,14 +6,9 @@ import androidx.media3.datasource.okhttp.OkHttpDataSource import androidx.media3.exoplayer.hls.HlsMediaSource import androidx.media3.exoplayer.source.MediaSource import androidx.media3.exoplayer.source.ProgressiveMediaSource -import androidx.media3.session.DefaultMediaNotificationProvider import androidx.media3.session.MediaSession import androidx.media3.session.MediaSessionService import com.vitorpamplona.amethyst.service.HttpClient -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch @UnstableApi // Extend MediaSessionService class PlaybackService : MediaSessionService() { @@ -28,36 +23,31 @@ class PlaybackService : MediaSessionService() { } fun newProgressiveDataSource(): MediaSource.Factory { - return ProgressiveMediaSource.Factory(VideoCache.get(applicationContext, HttpClient.getHttpClient())) + return ProgressiveMediaSource.Factory(VideoCache.get(Amethyst.instance, HttpClient.getHttpClient())) } // Create your Player and MediaSession in the onCreate lifecycle event - @kotlin.OptIn(DelicateCoroutinesApi::class) @OptIn(UnstableApi::class) override fun onCreate() { super.onCreate() - GlobalScope.launch(Dispatchers.IO) { - managerHls = MultiPlayerPlaybackManager(newHslDataSource(), videoViewedPositionCache) - managerProgressive = MultiPlayerPlaybackManager(newProgressiveDataSource(), videoViewedPositionCache) - managerLocal = MultiPlayerPlaybackManager(cachedPositions = videoViewedPositionCache) + managerHls = MultiPlayerPlaybackManager(newHslDataSource(), videoViewedPositionCache) + managerProgressive = MultiPlayerPlaybackManager(newProgressiveDataSource(), videoViewedPositionCache) + managerLocal = MultiPlayerPlaybackManager(cachedPositions = videoViewedPositionCache) - // Stop all videos and recreates all managers when the proxy changes. - HttpClient.proxyChangeListeners.add { - val toDestroyHls = managerHls - val toDestroyProgressive = managerProgressive + // Stop all videos and recreates all managers when the proxy changes. + HttpClient.proxyChangeListeners.add(this@PlaybackService::onProxyUpdated) + } - managerHls = MultiPlayerPlaybackManager(newHslDataSource(), videoViewedPositionCache) - managerProgressive = MultiPlayerPlaybackManager(newProgressiveDataSource(), videoViewedPositionCache) + private fun onProxyUpdated() { + val toDestroyHls = managerHls + val toDestroyProgressive = managerProgressive - toDestroyHls?.releaseAppPlayers() - toDestroyProgressive?.releaseAppPlayers() - } + managerHls = MultiPlayerPlaybackManager(newHslDataSource(), videoViewedPositionCache) + managerProgressive = MultiPlayerPlaybackManager(newProgressiveDataSource(), videoViewedPositionCache) - setMediaNotificationProvider( - DefaultMediaNotificationProvider.Builder(applicationContext).build() - ) - } + toDestroyHls?.releaseAppPlayers() + toDestroyProgressive?.releaseAppPlayers() } override fun onDestroy() { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt b/app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt index 0dadaaa6d..99c2bbea7 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt @@ -21,8 +21,7 @@ import okhttp3.OkHttpClient lateinit var cacheDataSourceFactory: CacheDataSource.Factory @Synchronized - @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) - private fun init(context: Context, client: OkHttpClient) { + fun initFileCache(context: Context) { exoDatabaseProvider = StandaloneDatabaseProvider(context) simpleCache = SimpleCache( @@ -30,8 +29,6 @@ import okhttp3.OkHttpClient leastRecentlyUsedCacheEvictor, exoDatabaseProvider ) - - renewCacheFactory(client) } // This method should be called when proxy setting changes. @@ -46,9 +43,12 @@ import okhttp3.OkHttpClient fun get(context: Context, client: OkHttpClient): CacheDataSource.Factory { if (!this::simpleCache.isInitialized) { - init(context, client) + initFileCache(context) } + // Renews the factory because OkHttpMight have changed. + renewCacheFactory(client) + return cacheDataSourceFactory } }