你如何在exoplayer中实现OkHttpDataSourceFactory?

How do you implement OkHttpDataSourceFactory in exoplayer?

如何在exoplayer中实现OkHttpDataSourceFactory?

我找遍了,但找不到关于如何实现 OkHttpDataSourceFactory 的好的答案。终于在其中一个关于其他人如何处理它的问题中找到了 example。这看起来很简单。我终于明白了。

val builder = OkHttpClient.Builder()
val client = builder.build()
val okHttpDataSourceFactory = OkHttpDataSourceFactory(client)
val player = SimpleExoPlayer.Builder(context)
        .setMediaSourceFactory(DefaultMediaSourceFactory(okHttpDataSourceFactory))
        .build()

您可以像往常一样对待生成器。在我的例子中,我需要添加一个拦截器来添加

builder.addInterceptor { chain ->
    val newRequest = chain.request().newBuilder()
            .addHeader("Authorization", String.format("Bearer %s", token))
            .build()
    chain.proceed(newRequest)
}