Skip to content

希望支持exoplayer的自定义数据源,默认的http客户端问题很多 #20

@onlymash

Description

@onlymash

默认的数据源所用的http客户端很多时候会被cf拦截,即使使用相同的请求头,指纹也有所区别,更换为okhttp可解决。
例如:

                   val okHttpClient = OkHttpClient.Builder()
                        .addInterceptor(OkHttpHeaderInterceptor(headers))
                        .build()
                    val httpDataSourceFactory = OkHttpDataSource.Factory(okHttpClient)
                    val cacheDataSourceFactory = CacheDataSource.Factory()
                        .setCache(cache)
                        .setUpstreamDataSourceFactory(httpDataSourceFactory)
                        .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR)
                    val mediaSourceFactory = ProgressiveMediaSource.Factory(cacheDataSourceFactory)

现有API没办法做到这一点,需要修改ExoPlayerMediampPlayer的构造函数,增加一个MediaSource.Factory,然后在setDataImpl使用。

override suspend fun setDataImpl(data: MediaData): ExoPlayerData = when (data) {
        is UriMediaData -> {
            ExoPlayerData(
                data,
                releaseResource = {
                    data.close()
                },
                setMedia = {
                    val item = MediaItem.Builder().apply {
                        setUri(data.uri)
                        setSubtitleConfigurations(
                            data.extraFiles.subtitles.map {
                                MediaItem.SubtitleConfiguration.Builder(
                                    it.uri.toUri(),
                                ).apply {
                                    it.label?.let { label -> setLabel(label) }
                                    it.mimeType?.let { mimeType -> setMimeType(mimeType) }
                                    it.language?.let { language -> setLanguage(language) }
                                }.build()
                            },
                        )
                    }.build()
                    withContext(Dispatchers.Main.immediate) {
                        exoPlayer.setMediaSource(mediaSourceFactory.createMediaSource(item))
                    }
                },
            )
        }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions