Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Comprehensive ExoPlayer Tutorial

Introduction to ExoPlayer

ExoPlayer is an open-source media player library for Android that provides a robust framework for playing audio and video content. It is highly customizable and supports features like adaptive streaming, DRM, and more.

Setting Up ExoPlayer

To get started with ExoPlayer, you need to add the ExoPlayer dependency to your Android project. Open your project's build.gradle file and add the following line in the dependencies section:

implementation 'com.google.android.exoplayer:exoplayer:2.15.1'

Creating a Simple ExoPlayer Instance

Creating an ExoPlayer instance involves a few steps. First, initialize the player in your activity or fragment:

private SimpleExoPlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

Preparing Media Source

ExoPlayer can play media from various sources. Here, we'll use a simple media URL:

String videoUrl = "https://www.example.com/video.mp4";
MediaItem mediaItem = MediaItem.fromUri(Uri.parse(videoUrl));
player.setMediaItem(mediaItem);
player.prepare();
player.play();
                

Adding ExoPlayer to Layout

To display the video, add a PlayerView to your layout XML file:

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

In your activity, bind the PlayerView to the ExoPlayer instance:

PlayerView playerView = findViewById(R.id.player_view);
playerView.setPlayer(player);
                

Handling Player Lifecycle

To ensure the player is properly released and resumed, handle its lifecycle methods:

@Override
protected void onStart() {
    super.onStart();
    if (Util.SDK_INT >= 24) {
        player.setPlayWhenReady(true);
    }
}

@Override
protected void onResume() {
    super.onResume();
    if ((Util.SDK_INT < 24 || player == null)) {
        player.setPlayWhenReady(true);
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (Util.SDK_INT < 24) {
        player.setPlayWhenReady(false);
    }
}

@Override
protected void onStop() {
    super.onStop();
    if (Util.SDK_INT >= 24) {
        player.setPlayWhenReady(false);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    player.release();
    player = null;
}
                

Advanced Features

ExoPlayer supports several advanced features such as DRM, adaptive streaming (HLS, DASH), and more. Here, we'll briefly touch on adaptive streaming:

String adaptiveUrl = "https://www.example.com/stream.mpd";
MediaItem mediaItem = MediaItem.fromUri(Uri.parse(adaptiveUrl));
player.setMediaItem(mediaItem);
player.prepare();
player.play();
                

Conclusion

ExoPlayer is a powerful and flexible media player library for Android. This tutorial covered the basics, including setting up ExoPlayer, preparing media sources, handling the player lifecycle, and touching on advanced features. With ExoPlayer, you can create a fully-featured media playback experience for your users.