ExoPlayer — The best video player for Android

Wajid Ali
3 min readFeb 25, 2023

In today’s world, video consumption has become an integral part of our daily lives. Whether it is streaming content online, watching tutorials, or simply catching up on our favorite TV shows, videos have become a preferred medium for information consumption. With the rise of mobile devices, video streaming has become more popular than ever, and developers are looking for ways to integrate the best video player into their Android applications. One such player is ExoPlayer, which has become a go-to choice for many Android developers.

ExoPlayer is an open-source media player library developed by Google, which supports playing audio and video both locally and over the network. It offers many advanced features, such as adaptive streaming, DRM, and closed captions, making it an ideal choice for developers who need to deliver high-quality video content in their applications. In this article, we will discuss how to integrate ExoPlayer into an Android app.

Step 1: Add ExoPlayer to your project The first step in integrating ExoPlayer is to add it as a dependency to your project. This can be done by adding the following line to your app-level build.gradle file:

implementation ‘com.google.android.exoplayer:exoplayer:2.X.X’

Note: Replace X.X with the version of ExoPlayer you want to use. You can find the latest version on the ExoPlayer GitHub page.

Step 2: Add ExoPlayerView to your layout The next step is to add the ExoPlayerView to your layout file. The ExoPlayerView is a custom view that provides a UI for controlling playback and displaying video content. You can add it to your layout file using the following code:

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

Note: You can customize the layout and styling of the ExoPlayerView to match your app’s design.

Step 3: Create an instance of ExoPlayer The next step is to create an instance of ExoPlayer. You can do this by calling the following code in your activity or fragment:

private lateinit var player: SimpleExoPlayer

private fun initializePlayer() {
player = SimpleExoPlayer.Builder(context).build()
}

Note: You can customize the player by configuring the SimpleExoPlayer.Builder object.

Step 4: Prepare the media source Before you can play a video, you need to prepare the media source. A media source is an abstraction of the media being played, and it can be a single file or a playlist. You can prepare the media source using the following code:

private fun prepareMediaSource() {  
val dataSourceFactory = DefaultDataSourceFactory(context, "user-agent")
val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse("https://example.com/video.mp4"))

player.prepare(mediaSource)

}

Note: You can customize the media source by using a different MediaSourceFactory and providing a different URI.

Step 5: Start and stop playback The final step is to start and stop playback. You can start playback using the following code:

private fun startPlayback() {
player.playWhenReady = true
}

You can stop playback using the following code:

private fun stopPlayback() {
player.playWhenReady = false
}

Note: You can also use the ExoPlayerView to control playback using its built-in UI controls.

In conclusion, ExoPlayer is a powerful and flexible media player library that offers advanced features and customization options.

Do comment as a token of encouragment to write on advanced concepts of ExoPlayer.

--

--

Wajid Ali

Android, iOS, Flutter, Augmented Reality and Technology enthusiast.