fix some SimpleCache issues

This commit is contained in:
jmir1 2022-01-05 04:06:03 +01:00
parent cc0646d529
commit 81d158a860

View file

@ -101,7 +101,7 @@ class PlayerActivity : AppCompatActivity() {
private lateinit var dataSourceFactory: DataSource.Factory private lateinit var dataSourceFactory: DataSource.Factory
private lateinit var dbProvider: StandaloneDatabaseProvider private lateinit var dbProvider: StandaloneDatabaseProvider
private val cacheSize = 100L * 1024L * 1024L // 100 MB 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 cacheFactory: CacheDataSource.Factory
private lateinit var mediaSourceFactory: MediaSourceFactory private lateinit var mediaSourceFactory: MediaSourceFactory
private lateinit var playerView: DoubleTapPlayerView private lateinit var playerView: DoubleTapPlayerView
@ -200,11 +200,15 @@ class PlayerActivity : AppCompatActivity() {
exoPlayer = newPlayer() exoPlayer = newPlayer()
dbProvider = StandaloneDatabaseProvider(baseContext) dbProvider = StandaloneDatabaseProvider(baseContext)
val cacheFolder = File(baseContext.filesDir, "media") val cacheFolder = File(baseContext.filesDir, "media")
simpleCache = SimpleCache( simpleCache = if (SimpleCache.isCacheFolderLocked(cacheFolder)) {
null
} else {
SimpleCache(
cacheFolder, cacheFolder,
LeastRecentlyUsedCacheEvictor(cacheSize), LeastRecentlyUsedCacheEvictor(cacheSize),
dbProvider dbProvider
) )
}
initPlayer() initPlayer()
} }
@ -234,11 +238,15 @@ class PlayerActivity : AppCompatActivity() {
dataSourceFactory = newDataSourceFactory() dataSourceFactory = newDataSourceFactory()
} }
logcat(LogPriority.INFO) { "playing $uri" } logcat(LogPriority.INFO) { "playing $uri" }
if (simpleCache != null) {
cacheFactory = CacheDataSource.Factory().apply { cacheFactory = CacheDataSource.Factory().apply {
setCache(simpleCache) setCache(simpleCache!!)
setUpstreamDataSourceFactory(dataSourceFactory) setUpstreamDataSourceFactory(dataSourceFactory)
} }
mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory) mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory)
} else {
mediaSourceFactory = DefaultMediaSourceFactory(dataSourceFactory)
}
mediaItem = MediaItem.Builder() mediaItem = MediaItem.Builder()
.setUri(uri) .setUri(uri)
.setMimeType(getMime(uri)) .setMimeType(getMime(uri))
@ -506,11 +514,15 @@ class PlayerActivity : AppCompatActivity() {
} else { } else {
newDataSourceFactory() newDataSourceFactory()
} }
if (simpleCache != null) {
cacheFactory = CacheDataSource.Factory().apply { cacheFactory = CacheDataSource.Factory().apply {
setCache(simpleCache) setCache(simpleCache!!)
setUpstreamDataSourceFactory(dataSourceFactory) setUpstreamDataSourceFactory(dataSourceFactory)
} }
mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory) mediaSourceFactory = DefaultMediaSourceFactory(cacheFactory)
} else {
mediaSourceFactory = DefaultMediaSourceFactory(dataSourceFactory)
}
exoPlayer.release() exoPlayer.release()
exoPlayer = newPlayer() exoPlayer = newPlayer()
exoPlayer.setMediaSource(mediaSourceFactory.createMediaSource(mediaItem), resumeAt) exoPlayer.setMediaSource(mediaSourceFactory.createMediaSource(mediaItem), resumeAt)
@ -598,7 +610,7 @@ class PlayerActivity : AppCompatActivity() {
override fun onDestroy() { override fun onDestroy() {
deletePendingEpisodes() deletePendingEpisodes()
releasePlayer() releasePlayer()
simpleCache.release() simpleCache?.release()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) {
finishAndRemoveTask() finishAndRemoveTask()
} }