ExoPlayer 2.18.2 基本使用(二)

ExoPlayer 2.18.2 基本使用(一)

上篇文章我簡(jiǎn)單介紹了基于2.18.2的Exoplayer的基本使用,這篇文章將介紹2.18.2版本下的Exoplayer緩存使用。
請(qǐng)注意,這里使用的是Exoplayer自帶的緩存機(jī)制,并不是使用的三方庫(kù)

江湖規(guī)矩,直接上代碼

  • 緩存工廠創(chuàng)建
object MediaCacheFactory {
    private const val TAG = "MediaCacheFactory"
    private var cacheFactory: DataSource.Factory? = null
    private val sDatabaseProvider: DatabaseProvider? = null

    @Synchronized
    fun getCacheFactory(ctx: Context): DataSource.Factory {
        if (cacheFactory == null) {
            var downDirectory = File(ctx.cacheDir, "videos")
            var cache = SimpleCache(
                downDirectory,
                LeastRecentlyUsedCacheEvictor(1024 * 1024 * 512),
                null
            )
            cacheFactory = CacheDataSource.Factory().setCache(cache)
                .setCacheReadDataSourceFactory(
                    DefaultDataSource.Factory(
                        ctx,
                        DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(false)
                            .setConnectTimeoutMs(8000)
                            .setReadTimeoutMs(8000)
                            .setUserAgent("MY_Exoplayer")
                    )
                )
                .setUpstreamDataSourceFactory(
                    DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(false)
                        .setConnectTimeoutMs(8000)
                        .setReadTimeoutMs(8000)
                        .setUserAgent("MY_Exoplayer")
                )
                .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
                .setEventListener(object : CacheDataSource.EventListener {
                    override fun onCachedBytesRead(cacheSizeBytes: Long, cachedBytesRead: Long) {
                        Log.d(TAG, "onCachedBytesRead $cacheSizeBytes  >> $cachedBytesRead")
                    }

                    override fun onCacheIgnored(reason: Int) {
                        Log.d(TAG, "onCacheIgnored $reason")
                    }

                })
        }
        return cacheFactory!!
    }
}
  • 緩存工廠的使用
val player = ExoPlayer.Builder(this).build()
playerView.player = player
val contentUri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
val mediaItem = MediaItem.fromUri(contentUri)
val mediaSource: ProgressiveMediaSource = ProgressiveMediaSource.Factory(MediaCacheFactory .getCacheFactory(context.applicationContext)).createMediaSource(mediaItem)
player.setMediaSource(mediaSource)
player.playWhenReady = true
player.prepare()

總結(jié)

至此就完成了Exoplayer自帶緩存的使用,下次再次播放相同視頻的時(shí)候,即使是沒有網(wǎng)絡(luò),也能輕松加載播放。
如果我的文章有幫助到你,麻煩一鍵三連,收藏點(diǎn)贊轉(zhuǎn)發(fā),Peace guys

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容