Search code examples
androidkotlinandroid-videoviewexoplayerexoplayer2.x

How do you implement OkHttpDataSourceFactory in exoplayer?


How do you implement OkHttpDataSourceFactory in exoplayer?


Solution

  • I looked all over and I could not find good answer on how to implement the OkHttpDataSourceFactory. Finally found an example in one of the issues on how someone else went about it. It seemed simple enough. I finally got it down to this.

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

    You can treat the builder as you normally would. In my case I need to add an interceptor to add

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