0

i am trying to play a hls uri with exoplayer in java

whem i give a uri with https protocol it plays well but in case of http protocol the content is not ben played

but when i play the same links with other players like mxplayer or vlc media player it is played well

can anypne help

`ExoPlayer player = new ExoPlayer.Builder(this).build();

    binding.playerView.setPlayer(player);

    MediaItem mediaItem = MediaItem.fromUri("http://cdnapi.kaltura.com/p/1878761/sp/187876100/playManifest/entryId/1_usagz19w/flavorIds/1_5spqkazq,1_nslowvhp,1_boih5aji,1_qahc37ag/format/applehttp/protocol/http/a.m3u8");
    player.addMediaItem(mediaItem);

    player.prepare();

    player.setPlayWhenReady(true);`

2 Answers2

0

XML file.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--Widget for exoplayer view-->
    <com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/exoPlayerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Class file-

class MainActivity : AppCompatActivity() {
    var playerView: StyledPlayerView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
      playerView = findViewById(R.id.exoPlayerView)
        val dataSourceFactory: DataSource.Factory = DefaultHttpDataSource.Factory()
        val hlsMediaSource: HlsMediaSource = HlsMediaSource.Factory(dataSourceFactory)
            .createMediaSource(MediaItem.fromUri("http://cdnapi.kaltura.com/p/1878761/sp/187876100/playManifest/entryId/1_usagz19w/flavorIds/1_5spqkazq,1_nslowvhp,1_boih5aji,1_qahc37ag/format/applehttp/protocol/http/a.m3u8"))
        val player = ExoPlayer.Builder(this).build()
        player.setMediaSource(hlsMediaSource)
        playerView?.player = player
        player.prepare()
        player.playWhenReady = true
    }
}

Manifest File-

   <uses-permission android:name="android.permission.INTERNET" />

and add this code

Sandesh Khutal
  • 1,712
  • 1
  • 4
  • 17
0

By default, ExoPlayer will not play HTTP content now - many players and browser now impose this restriction as their standard behaviour.

From the current , at the the time of writing ExoPlayer documentation (https://exoplayer.dev/troubleshooting.html):

Fixing “Cleartext HTTP traffic not permitted” errors

This error will occur if your app requests cleartext HTTP traffic (i.e., http:// rather than https://) when its Network Security Configuration does not permit it. If your app targets Android 9 (API level 28) or later, cleartext HTTP traffic is disabled by the default configuration.

If your app needs to work with cleartext HTTP traffic then you need to use a Network Security Configuration that permits it. Please see Android’s network security documentation for details. To enable all cleartext HTTP traffic, you can simply add android:usesCleartextTraffic="true" to the application element of your app’s AndroidManifest.xml.

The ExoPlayer demo app uses the default Network Security Configuration, and so does not allow cleartext HTTP traffic. You can enable it using the instructions above.

As a first step you can try as they suggest and add 'android:usesCleartextTraffic="true"' to your manifest.

Mick
  • 24,231
  • 1
  • 54
  • 120