From 121f9d5bec72d454a0a154d38636972937ab833d Mon Sep 17 00:00:00 2001 From: jmir1 Date: Wed, 28 Jul 2021 17:37:05 +0200 Subject: [PATCH] Player optimizations: avoid deprecated CacheDat... ...aSourceFactory and avoid crashes --- .../tachiyomi/ui/player/PlayerActivity.kt | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt index d9a084807..ebbe71c47 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt @@ -31,7 +31,7 @@ import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory import com.google.android.exoplayer2.upstream.DefaultHttpDataSource import com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException import com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException -import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory +import com.google.android.exoplayer2.upstream.cache.CacheDataSource import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor import com.google.android.exoplayer2.upstream.cache.SimpleCache import com.google.android.exoplayer2.util.Clock @@ -83,7 +83,7 @@ class PlayerActivity : AppCompatActivity() { private lateinit var dbProvider: ExoDatabaseProvider val cacheSize = 100L * 1024L * 1024L // 100 mb private lateinit var simpleCache: SimpleCache - private lateinit var cacheFactory: CacheDataSourceFactory + private lateinit var cacheFactory: CacheDataSource.Factory private lateinit var mediaSourceFactory: MediaSourceFactory private lateinit var playerView: DoubleTapPlayerView private lateinit var youTubeDoubleTap: YouTubeOverlay @@ -149,8 +149,16 @@ class PlayerActivity : AppCompatActivity() { userAgentString = WebSettings.getDefaultUserAgent(this) videos = runBlocking { awaitVideoList() } if (videos.isEmpty()) { - baseContext.toast("Cannot play episode") - super.onBackPressed() + mediaSourceFactory = DefaultMediaSourceFactory(DefaultDataSourceFactory(this)) + exoPlayer = newPlayer() + dbProvider = ExoDatabaseProvider(baseContext) + simpleCache = SimpleCache( + File(baseContext.filesDir, "media"), + LeastRecentlyUsedCacheEvictor(cacheSize), + dbProvider + ) + finish() + return } if (videos.lastIndex > 0) settingsBtn.visibility = View.VISIBLE dbProvider = ExoDatabaseProvider(baseContext) @@ -167,7 +175,10 @@ class PlayerActivity : AppCompatActivity() { uri = videos.first().videoUrl!! dataSourceFactory = newDataSourceFactory() } - cacheFactory = CacheDataSourceFactory(simpleCache, dataSourceFactory) + cacheFactory = CacheDataSource.Factory().apply { + setCache(simpleCache) + setUpstreamDataSourceFactory(dataSourceFactory) + } mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory) mediaItem = MediaItem.Builder() .setUri(uri) @@ -249,7 +260,15 @@ class PlayerActivity : AppCompatActivity() { } private suspend fun awaitVideoList(): List