Implementing ExoPlayer Using Android Media3 — Part I

Anand Jeyapal
2 min readJul 29, 2024

Android’s Media3 library is the evolution of the previous ExoPlayer library, integrating it more closely with the Jetpack suite and offering enhanced features and flexibility.

This guide will walk you through the steps to implement ExoPlayer using the Android Media3 library.

Setting up Project

First, create a new Android project or open an existing one in Android Studio. Ensure that you have the required dependencies added to your build.gradle file as mentioned below.

dependencies {
implementation 'androidx.media3:media3-exoplayer:1.0.0-beta02'
implementation 'androidx.media3:media3-ui:1.0.0-beta02'
}

Setting up AndroidManifest

Make sure you have added android.permission.INTERNET permission in your manifest file as we are going to play the video from a network source.

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

Design the Layout

Create a layout file for your activity, activity_main.xml, and add a PlayerView to display the video.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.media3.ui.PlayerView
android:id="@+id/player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />

</RelativeLayout>

Initialize the ExoPlayer

In your MainActivity, initialize ExoPlayer and set it up with the PlayerView.

import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView

class MainActivity : AppCompatActivity() {

private lateinit var player: ExoPlayer
private lateinit var playerView: PlayerView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Initialize the PlayerView
playerView = findViewById(R.id.player_view)

// Initialize the ExoPlayer
player = ExoPlayer.Builder(this).build()
playerView.player = player

// Prepare media item
val mediaItem = MediaItem.fromUri(Uri.parse("https://www.example.com/media.mp4"))
player.setMediaItem(mediaItem)
player.prepare()
player.play()
}

override fun onStop() {
super.onStop()
player.release()
}
}

Handle Lifecycle Events

To ensure proper resource management, handle the player lifecycle by releasing the player in onStop or onDestroy and re-initializing it if necessary.

override fun onStop() {
super.onStop()
player.release()
}

For more complex use cases, consider pausing the player in onPause and resuming it in onResume.

override fun onPause() {
super.onPause()
player.pause()
}

override fun onResume() {
super.onResume()
player.play()
}

Result

Hooray!! You’ve implemented the basic video player, grab a coffee ☕ and be proud.

Conclusion

Implementing ExoPlayer with Android Media3 provides a robust and flexible media playback solution for your Android apps. By following the steps outlined in this guide, you can set up a basic functional media player. I will cover the further customization of controls and enhancements in the next post.

--

--