ExoPlayer app to play HLS videos

To create an ExoPlayer demo app to play HLS videos, you can follow the steps below:

  1. Create a new Android project in Android Studio.

  2. Add the ExoPlayer library to your project by adding the following dependency to your app's build.gradle file:

implementation 'com.google.android.exoplayer:exoplayer:2.16.0'
  1. Create a layout file for your video player activity. Here's an example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
  1. In your activity's onCreate method, initialize the ExoPlayer and set the player's view:

// Initialize the ExoPlayer
SimpleExoPlayer player = new SimpleExoPlayer.Builder(this).build();

// Set the player's view
PlayerView playerView = findViewById(R.id.player_view);
playerView.setPlayer(player);
  1. Create a MediaSource object to represent your HLS video stream. Here's an example:

// Create a MediaSource object for the HLS video stream
String videoUrl = "https://endpoint.dyntube.com/video.m3u8";
Uri uri = Uri.parse(videoUrl);
MediaSource mediaSource = new HlsMediaSource.Factory(
        new DefaultDataSourceFactory(this, "exoplayer-demo")).createMediaSource(uri);

Note that in this example, the HLS video stream is hosted at https://endpoint.dyntube.com/video.m3u8. You should replace this URL with the URL for your own video stream.

  1. Set the MediaSource on the ExoPlayer:

player.setMediaSource(mediaSource);
player.prepare();
  1. Start the video playback:

player.setPlayWhenReady(true);

Last updated