'Exoplayer Play From Http (non Https) Urls with Media Items or Media Source [ 2022 ]

I want to play videos from urls using http protocol inside exoplayer in android app using JAVA.

i had tried the below method in manifest

 android:usesCleartextTraffic="true"

that does not work for me on minimumsdk 23.

I have tested in android v7.1 and android 11.

Later i had came to know that i have to use DefaultHttpFactory / OKhttp from here

But i cant get good samples or implementation over internet.

I had implemented okhttp in gradle

    implementation 'com.google.android.exoplayer:extension-okhttp:2.x.x'

but i dont know how to implement this methods or function in JAVA.

How to solve this with okhttp or is there a better solution?

Guys i found the solution :

This works for me with out any external library for HLS.

DataSource.Factory dataSourceFactory = new DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(true);
HlsMediaSource hlsMediaSource =
        new HlsMediaSource.Factory(dataSourceFactory)
                .setAllowChunklessPreparation(false) // use it only if needed
                .createMediaSource(MediaItem.fromUri(uri)); // pass your url
exoPlayer.addMediaSource(hlsMediaSource);
exoPlayer.prepare();
exoPlayer.setPlayWhenReady(true);
exoPlayer.play();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source