Initializes the VideoCache when creating the app so that the PlaybackService doesn't suffer with delays in accessing the disk to create the cache.
This commit is contained in:
@@ -6,10 +6,13 @@ import android.os.StrictMode.ThreadPolicy
|
|||||||
import android.os.StrictMode.VmPolicy
|
import android.os.StrictMode.VmPolicy
|
||||||
|
|
||||||
class Amethyst : Application() {
|
class Amethyst : Application() {
|
||||||
|
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
instance = this
|
instance = this
|
||||||
|
|
||||||
|
VideoCache.initFileCache(instance)
|
||||||
|
|
||||||
if (BuildConfig.DEBUG) {
|
if (BuildConfig.DEBUG) {
|
||||||
StrictMode.setThreadPolicy(
|
StrictMode.setThreadPolicy(
|
||||||
ThreadPolicy.Builder()
|
ThreadPolicy.Builder()
|
||||||
|
|||||||
@@ -6,14 +6,9 @@ import androidx.media3.datasource.okhttp.OkHttpDataSource
|
|||||||
import androidx.media3.exoplayer.hls.HlsMediaSource
|
import androidx.media3.exoplayer.hls.HlsMediaSource
|
||||||
import androidx.media3.exoplayer.source.MediaSource
|
import androidx.media3.exoplayer.source.MediaSource
|
||||||
import androidx.media3.exoplayer.source.ProgressiveMediaSource
|
import androidx.media3.exoplayer.source.ProgressiveMediaSource
|
||||||
import androidx.media3.session.DefaultMediaNotificationProvider
|
|
||||||
import androidx.media3.session.MediaSession
|
import androidx.media3.session.MediaSession
|
||||||
import androidx.media3.session.MediaSessionService
|
import androidx.media3.session.MediaSessionService
|
||||||
import com.vitorpamplona.amethyst.service.HttpClient
|
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
|
@UnstableApi // Extend MediaSessionService
|
||||||
class PlaybackService : MediaSessionService() {
|
class PlaybackService : MediaSessionService() {
|
||||||
@@ -28,36 +23,31 @@ class PlaybackService : MediaSessionService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun newProgressiveDataSource(): MediaSource.Factory {
|
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
|
// Create your Player and MediaSession in the onCreate lifecycle event
|
||||||
@kotlin.OptIn(DelicateCoroutinesApi::class)
|
|
||||||
@OptIn(UnstableApi::class)
|
@OptIn(UnstableApi::class)
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
|
||||||
GlobalScope.launch(Dispatchers.IO) {
|
managerHls = MultiPlayerPlaybackManager(newHslDataSource(), videoViewedPositionCache)
|
||||||
managerHls = MultiPlayerPlaybackManager(newHslDataSource(), videoViewedPositionCache)
|
managerProgressive = MultiPlayerPlaybackManager(newProgressiveDataSource(), videoViewedPositionCache)
|
||||||
managerProgressive = MultiPlayerPlaybackManager(newProgressiveDataSource(), videoViewedPositionCache)
|
managerLocal = MultiPlayerPlaybackManager(cachedPositions = videoViewedPositionCache)
|
||||||
managerLocal = MultiPlayerPlaybackManager(cachedPositions = videoViewedPositionCache)
|
|
||||||
|
|
||||||
// Stop all videos and recreates all managers when the proxy changes.
|
// Stop all videos and recreates all managers when the proxy changes.
|
||||||
HttpClient.proxyChangeListeners.add {
|
HttpClient.proxyChangeListeners.add(this@PlaybackService::onProxyUpdated)
|
||||||
val toDestroyHls = managerHls
|
}
|
||||||
val toDestroyProgressive = managerProgressive
|
|
||||||
|
|
||||||
managerHls = MultiPlayerPlaybackManager(newHslDataSource(), videoViewedPositionCache)
|
private fun onProxyUpdated() {
|
||||||
managerProgressive = MultiPlayerPlaybackManager(newProgressiveDataSource(), videoViewedPositionCache)
|
val toDestroyHls = managerHls
|
||||||
|
val toDestroyProgressive = managerProgressive
|
||||||
|
|
||||||
toDestroyHls?.releaseAppPlayers()
|
managerHls = MultiPlayerPlaybackManager(newHslDataSource(), videoViewedPositionCache)
|
||||||
toDestroyProgressive?.releaseAppPlayers()
|
managerProgressive = MultiPlayerPlaybackManager(newProgressiveDataSource(), videoViewedPositionCache)
|
||||||
}
|
|
||||||
|
|
||||||
setMediaNotificationProvider(
|
toDestroyHls?.releaseAppPlayers()
|
||||||
DefaultMediaNotificationProvider.Builder(applicationContext).build()
|
toDestroyProgressive?.releaseAppPlayers()
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
|||||||
@@ -21,8 +21,7 @@ import okhttp3.OkHttpClient
|
|||||||
lateinit var cacheDataSourceFactory: CacheDataSource.Factory
|
lateinit var cacheDataSourceFactory: CacheDataSource.Factory
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
fun initFileCache(context: Context) {
|
||||||
private fun init(context: Context, client: OkHttpClient) {
|
|
||||||
exoDatabaseProvider = StandaloneDatabaseProvider(context)
|
exoDatabaseProvider = StandaloneDatabaseProvider(context)
|
||||||
|
|
||||||
simpleCache = SimpleCache(
|
simpleCache = SimpleCache(
|
||||||
@@ -30,8 +29,6 @@ import okhttp3.OkHttpClient
|
|||||||
leastRecentlyUsedCacheEvictor,
|
leastRecentlyUsedCacheEvictor,
|
||||||
exoDatabaseProvider
|
exoDatabaseProvider
|
||||||
)
|
)
|
||||||
|
|
||||||
renewCacheFactory(client)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method should be called when proxy setting changes.
|
// This method should be called when proxy setting changes.
|
||||||
@@ -46,9 +43,12 @@ import okhttp3.OkHttpClient
|
|||||||
|
|
||||||
fun get(context: Context, client: OkHttpClient): CacheDataSource.Factory {
|
fun get(context: Context, client: OkHttpClient): CacheDataSource.Factory {
|
||||||
if (!this::simpleCache.isInitialized) {
|
if (!this::simpleCache.isInitialized) {
|
||||||
init(context, client)
|
initFileCache(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Renews the factory because OkHttpMight have changed.
|
||||||
|
renewCacheFactory(client)
|
||||||
|
|
||||||
return cacheDataSourceFactory
|
return cacheDataSourceFactory
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user