Where and how do we set cloudfront cookies for HLS streaming in Exoplayer Android library. Is it with DEFAULT COOKIES MANAGER? What is exact way to set cookies. Till now no exact solutions from any links
Try to do set cookies in DefaultHttpDataSourceFactory.
https://github.com/google/ExoPlayer/issues/4870#issuecomment-425795374
Recent versions of exoplayer is little different from the above mentioned code.here I have posted a simple code that uses a cloudfront signed HLS m3u8 as media source.
---------- Gradle -----------
def exoplayer_version = "2.17.1"
implementation "com.google.android.exoplayer:exoplayer-hls:$exoplayer_version"
implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version"
implementation "com.google.android.exoplayer:exoplayer-ui:$exoplayer_version"
----------- XML [define a ui.PlayerView] -----------
[....]
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/ep_video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
[....]
------------ Fragment ------------
private var exoPlayer: ExoPlayer? = null
[...]
val sourceURL= "https://a.cloudfront.net/b.m3u8?Policy=eyJT..._&Signature=eZ6....__&Key-Pair-Id=KRSDW..."
// extract Policy, Signature and KeyPairIds
val uri: Uri = Uri.parse(sourceURL)
val policyString: String? = uri.getQueryParameter("Policy")
val signatureString: String? = uri.getQueryParameter("Signature")
val keyPairIdString: String? = uri.getQueryParameter("Key-Pair-Id")
// Create a cookie list to send
var cookieValue = ""
cookieValue += "CloudFront-Policy=$policyString;"
cookieValue += "CloudFront-Signature=$signatureString;"
cookieValue += "CloudFront-Key-Pair-Id=$keyPairIdString;"
// httpDataSourceFactory
val httpDataSourceFactory: HttpDataSource.Factory =
DefaultHttpDataSource.Factory()
.setDefaultRequestProperties(mapOf("Cookie" to cookieValue))
.setAllowCrossProtocolRedirects(true)
val dataSourceFactory = DefaultDataSource.Factory(requireContext(),
httpDataSourceFactory)
val hlsMediaSource: HlsMediaSource =
HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(sourceURL))
exoPlayer = ExoPlayer.Builder(requireContext()).build()
exoPlayer.also { exoPlayer ->
binding.epVideoView.player = exoPlayer
exoPlayer?.addMediaSource(hlsMediaSource)
exoPlayer?.prepare()
exoPlayer?.playWhenReady = true
}
[...]
Incase if you get any 403 from exoplayer, try changing the userAgent in httpDataSourceFactory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With