Skip to content

Commit

Permalink
fix some SimpleCache issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jmir1 committed Jan 5, 2022
1 parent cc0646d commit 81d158a
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class PlayerActivity : AppCompatActivity() {
private lateinit var dataSourceFactory: DataSource.Factory
private lateinit var dbProvider: StandaloneDatabaseProvider
private val cacheSize = 100L * 1024L * 1024L // 100 MB
private lateinit var simpleCache: SimpleCache
private var simpleCache: SimpleCache? = null
private lateinit var cacheFactory: CacheDataSource.Factory
private lateinit var mediaSourceFactory: MediaSourceFactory
private lateinit var playerView: DoubleTapPlayerView
Expand Down Expand Up @@ -200,11 +200,15 @@ class PlayerActivity : AppCompatActivity() {
exoPlayer = newPlayer()
dbProvider = StandaloneDatabaseProvider(baseContext)
val cacheFolder = File(baseContext.filesDir, "media")
simpleCache = SimpleCache(
cacheFolder,
LeastRecentlyUsedCacheEvictor(cacheSize),
dbProvider
)
simpleCache = if (SimpleCache.isCacheFolderLocked(cacheFolder)) {
null
} else {
SimpleCache(
cacheFolder,
LeastRecentlyUsedCacheEvictor(cacheSize),
dbProvider
)
}

initPlayer()
}
Expand Down Expand Up @@ -234,11 +238,15 @@ class PlayerActivity : AppCompatActivity() {
dataSourceFactory = newDataSourceFactory()
}
logcat(LogPriority.INFO) { "playing $uri" }
cacheFactory = CacheDataSource.Factory().apply {
setCache(simpleCache)
setUpstreamDataSourceFactory(dataSourceFactory)
if (simpleCache != null) {
cacheFactory = CacheDataSource.Factory().apply {
setCache(simpleCache!!)
setUpstreamDataSourceFactory(dataSourceFactory)
}
mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory)
} else {
mediaSourceFactory = DefaultMediaSourceFactory(dataSourceFactory)
}
mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory)
mediaItem = MediaItem.Builder()
.setUri(uri)
.setMimeType(getMime(uri))
Expand Down Expand Up @@ -506,11 +514,15 @@ class PlayerActivity : AppCompatActivity() {
} else {
newDataSourceFactory()
}
cacheFactory = CacheDataSource.Factory().apply {
setCache(simpleCache)
setUpstreamDataSourceFactory(dataSourceFactory)
if (simpleCache != null) {
cacheFactory = CacheDataSource.Factory().apply {
setCache(simpleCache!!)
setUpstreamDataSourceFactory(dataSourceFactory)
}
mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory)
} else {
mediaSourceFactory = DefaultMediaSourceFactory(dataSourceFactory)
}
mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory)
exoPlayer.release()
exoPlayer = newPlayer()
exoPlayer.setMediaSource(mediaSourceFactory.createMediaSource(mediaItem), resumeAt)
Expand Down Expand Up @@ -598,7 +610,7 @@ class PlayerActivity : AppCompatActivity() {
override fun onDestroy() {
deletePendingEpisodes()
releasePlayer()
simpleCache.release()
simpleCache?.release()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
finishAndRemoveTask()
}
Expand Down

0 comments on commit 81d158a

Please sign in to comment.