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 32d267027..fbaa396ec 100644 --- a/app/src/main/java/io/legado/app/help/ExoPlayerHelper.kt +++ b/app/src/main/java/io/legado/app/help/ExoPlayerHelper.kt @@ -10,18 +10,16 @@ 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 io.legado.app.help.http.okHttpClient object ExoPlayerHelper { fun createMediaSource( uri: Uri, - overrideExtension: String? = null, - userAgent: String + dataSourceFactory: OkHttpDataSource.Factory, + overrideExtension: String? = null ): MediaSource { val mediaItem = MediaItem.fromUri(uri) - val dataSourceFactory = OkHttpDataSource.Factory(okHttpClient).setUserAgent(userAgent) val mediaSourceFactory = when (inferContentType(uri, overrideExtension)) { C.TYPE_SS -> SsMediaSource.Factory(dataSourceFactory) C.TYPE_DASH -> DashMediaSource.Factory(dataSourceFactory) 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 a2a331ead..b0e60648e 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -16,6 +16,7 @@ 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 @@ -27,6 +28,7 @@ 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 @@ -133,8 +135,10 @@ class AudioPlayService : BaseService(), useWebView = true ) val uri = Uri.parse(analyzeUrl.url) + val dataSourceFactory = OkHttpDataSource.Factory(okHttpClient) + .setDefaultRequestProperties(analyzeUrl.headerMap) val mediaSource = ExoPlayerHelper - .createMediaSource(uri, userAgent = analyzeUrl.getUserAgent()) + .createMediaSource(uri, dataSourceFactory) exoPlayer.setMediaSource(mediaSource) exoPlayer.playWhenReady = true exoPlayer.prepare()