diff --git a/app/build.gradle b/app/build.gradle index e1d5b3c1d..1ec179663 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -153,10 +153,10 @@ dependencies { def exoplayer_version = '2.15.0' implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version" implementation "com.google.android.exoplayer:extension-okhttp:$exoplayer_version" - implementation "com.google.android.exoplayer:exoplayer-hls:$exoplayer_version" - implementation "com.google.android.exoplayer:exoplayer-smoothstreaming:$exoplayer_version" - implementation "com.google.android.exoplayer:exoplayer-dash:$exoplayer_version" - implementation "com.google.android.exoplayer:exoplayer-rtsp:$exoplayer_version" +// implementation "com.google.android.exoplayer:exoplayer-hls:$exoplayer_version" +// implementation "com.google.android.exoplayer:exoplayer-smoothstreaming:$exoplayer_version" +// implementation "com.google.android.exoplayer:exoplayer-dash:$exoplayer_version" +// implementation "com.google.android.exoplayer:exoplayer-rtsp:$exoplayer_version" //Splitties def splitties_version = '2.1.1' diff --git a/app/src/main/java/io/legado/app/help/ExoPlayerHelper.kt b/app/src/main/java/io/legado/app/help/ExoPlayerHelper.kt index fbaa396ec..a7d653b6f 100644 --- a/app/src/main/java/io/legado/app/help/ExoPlayerHelper.kt +++ b/app/src/main/java/io/legado/app/help/ExoPlayerHelper.kt @@ -1,33 +1,68 @@ package io.legado.app.help import android.net.Uri -import com.google.android.exoplayer2.C import com.google.android.exoplayer2.MediaItem +import com.google.android.exoplayer2.database.ExoDatabaseProvider import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource import com.google.android.exoplayer2.source.MediaSource import com.google.android.exoplayer2.source.ProgressiveMediaSource -import com.google.android.exoplayer2.source.dash.DashMediaSource -import com.google.android.exoplayer2.source.hls.HlsMediaSource -import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource -import com.google.android.exoplayer2.util.Util.inferContentType +import com.google.android.exoplayer2.upstream.cache.Cache +import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor +import com.google.android.exoplayer2.upstream.cache.SimpleCache +import io.legado.app.help.http.okHttpClient +import splitties.init.appCtx +import java.io.File object ExoPlayerHelper { fun createMediaSource( uri: Uri, - dataSourceFactory: OkHttpDataSource.Factory, + defaultRequestProperties: Map, overrideExtension: String? = null ): MediaSource { val mediaItem = MediaItem.fromUri(uri) - val mediaSourceFactory = when (inferContentType(uri, overrideExtension)) { - C.TYPE_SS -> SsMediaSource.Factory(dataSourceFactory) - C.TYPE_DASH -> DashMediaSource.Factory(dataSourceFactory) - C.TYPE_HLS -> HlsMediaSource.Factory(dataSourceFactory) - else -> ProgressiveMediaSource.Factory(dataSourceFactory) - } + val mediaSourceFactory = ProgressiveMediaSource.Factory( + cacheDataSourceFactory.setDefaultRequestProperties(defaultRequestProperties) + ) return mediaSourceFactory.createMediaSource(mediaItem) } + + /** + * 支持缓存的DataSource.Factory + */ + private val cacheDataSourceFactory by lazy { + //使用自定义的CacheDataSource以支持设置UA + return@lazy OkhttpCacheDataSource.Factory() + .setCache(cache) + .setUpstreamDataSourceFactory(okhttpDataFactory) + + } + + /** + * Okhttp DataSource.Factory + */ + private val okhttpDataFactory by lazy { + OkHttpDataSource.Factory(okHttpClient) + + } + + /** + * Exoplayer 内置的缓存 + */ + private val cache: Cache by lazy { + val databaseProvider = ExoDatabaseProvider(appCtx) + return@lazy SimpleCache( + //Exoplayer的缓存路径 + File(appCtx.externalCacheDir, "exoplayer"), + //100M的缓存 + LeastRecentlyUsedCacheEvictor((100 * 1024 * 1024).toLong()), + //记录缓存的数据库 + databaseProvider + ); + + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index b0e60648e..79e1ac774 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -16,7 +16,6 @@ import androidx.media.AudioFocusRequestCompat import com.google.android.exoplayer2.PlaybackException import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.SimpleExoPlayer -import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource import io.legado.app.R import io.legado.app.base.BaseService import io.legado.app.constant.AppConst @@ -28,7 +27,6 @@ import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.ExoPlayerHelper import io.legado.app.help.MediaHelp -import io.legado.app.help.http.okHttpClient import io.legado.app.model.AudioPlay import io.legado.app.model.ReadAloud import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -135,10 +133,8 @@ class AudioPlayService : BaseService(), useWebView = true ) val uri = Uri.parse(analyzeUrl.url) - val dataSourceFactory = OkHttpDataSource.Factory(okHttpClient) - .setDefaultRequestProperties(analyzeUrl.headerMap) val mediaSource = ExoPlayerHelper - .createMediaSource(uri, dataSourceFactory) + .createMediaSource(uri, analyzeUrl.headerMap) exoPlayer.setMediaSource(mediaSource) exoPlayer.playWhenReady = true exoPlayer.prepare()