Exo-Player 2.10.1 说 SimpleCache(File cacheDir, CacheEvictor evictor) 已弃用

Nic*_*apu 4 android kotlin exoplayer exoplayer2.x

我正在使用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!!
}
Run Code Online (Sandbox Code Playgroud)

在这段代码中,编译器只是警告我构造函数 SimpleCache(File!, CacheEvictor!) 已被弃用。在 Java 中已弃用。所以,在那之后我试图在文件中找到方法,现在SimpleCache.java有一种使用方法SimpleCache是..

SimpleCache(File cacheDir, CacheEvictor evictor, DatabaseProvider databaseProvider)
Run Code Online (Sandbox Code Playgroud)

它对我来说是新的。所以,我的问题是如何使用这个新的构造函数而不是旧的不推荐使用的构造函数?有办法通过DatabaseProvider吗?如果是,那么如何或从什么?

我正在使用ExoPlayer库:

implementation 'com.google.android.exoplayer:exoplayer:2.10.1'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.10.1'
Run Code Online (Sandbox Code Playgroud)

并且因为这个版本提供了易于创建ExoPlayerFactory instancebuild ProgressiveMediaSource.

Nav*_*Dew 12

尝试这个:

        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)
Run Code Online (Sandbox Code Playgroud)

  • 'ExoDatabaseProvider' 已弃用,您可以将其替换为 StandaloneDatabaseProvider(mContext) (4认同)