我正在使用 ExoPlayer
从在线服务器播放视频。所以,为了更好地保存更多的互联网或数据回放视频,我只是将所有视频缓存到 Cache directory
.但问题是它的说法已弃用 constructor
的 SimpleCache
.
查看我的代码:
private var sDownloadCache: SimpleCache? = null
fun getInstance(mContext: Context): SimpleCache {
val cacheEvictor = LeastRecentlyUsedCacheEvictor((100 * 1024 * 1024).toLong())
if (sDownloadCache == null)
sDownloadCache = SimpleCache(File(mContext.getCacheDir(), "media"),
cacheEvictor)
return sDownloadCache!!
}
在这段代码中,编译器只是警告我构造函数 SimpleCache(File!, CacheEvictor!) 已弃用。 Java 中已弃用 .所以,在那之后,我试图在
SimpleCache.java
的文件中找到方法。有一种方法可以使用SimpleCache
现在是..SimpleCache(File cacheDir, CacheEvictor evictor, DatabaseProvider databaseProvider)
它对我来说是新的。所以,我的问题是如何使用这个新的构造函数而不是旧的不推荐使用的构造函数?有没有办法通过
DatabaseProvider
?如果是,那么如何或从什么?我正在使用
ExoPlayer
图书馆:implementation 'com.google.android.exoplayer:exoplayer:2.10.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.10.1'
因为这个版本提供了易于创建的
ExoPlayerFactory
instance
和 build ProgressiveMediaSource
.
最佳答案
尝试这个:
val evictor = LeastRecentlyUsedCacheEvictor((100 * 1024 * 1024).toLong())
val databaseProvider: DatabaseProvider = ExoDatabaseProvider(mContext)
simpleCache = SimpleCache(File(mContext.cacheDir, "media"), evictor, databaseProvider)
val mediaSource = ProgressiveMediaSource.Factory(
simpleCache?.let {
CacheDataSourceFactory(
mContext,
100 * 1024 * 1024, 10 * 1024 * 1024, playUri, it
)
}
)
.createMediaSource(playUri)
exoPlayer?.prepare(mediaSource)
关于android - Exo-Player 2.10.1 说 SimpleCache(File cacheDir, CacheEvictor evictor) 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159372/