ExoPlayer in Android with Example
Last Updated :
23 Jul, 2025
ExoPlayerView is one of the most used UI components in many apps such as YouTube, Netflix, and many video streaming platforms. ExoPlayerView is used for audio as well as video streaming in Android apps. Many Google apps use ExoPlayerView for streaming audios and videos. ExoPlayer is a media player library that provides a way to play audio and video with lots of customization in it. It is an alternative that is used to play videos and audios in Android along with MediaPlayer. ExoPlayer is a library that is the best alternative source for playing audio and videos on Android. This library will also help you to customize your media player according to our requirements.
Advantages of Using ExoPlayer
- ExoPlayer provides the support for the playlist and with this, you can clip or merge your media.
- With the help of ExoPlayer, you can directly fetch media files such as audios and videos directly from the internet and play them inside the ExoPlayer.
- It provides smooth encryption and streaming of video and audio files.
- ExoPlayer provides you the ability to customize your media player according to your requirements.
ExoPlayer vs MediaPlayer
ExoPlayer | MediaPlayer |
---|
ExoPlayer supports dynamic streaming over HTTP. | MediaPlayer does not support dynamic support over HTTP. |
It provides smooth streaming and encryption for the played video. | MediaPlayer does not provide smooth streaming and encryption for the video. |
ExoPlayer provides support to clip or merge your media files. | MediaPlayer does not provide any support for clipping and merging of media files. |
ExoPlayer can be customized according to our requirements. | MediaPlayer cannot be customized according to our requirements. |
ExoPlayer is able to stream audio and video files directly from the server without downloading. | Media Player is not able to play audio and video files directly from the server. |
ExoPlayer is released in API level 16 and it will not work on the device below API level 16. | Media Player was released in API level 1 and it works on all devices. |
ExoPlayer easily handles buffering of audio and video files. | Media Player is not able to handle buffering of audio and videos. |
Step by Step Implementation of ExoPlayer in Android
We will be creating a simple video player app in which we will be fetching a video from a URL and play that video inside our ExoPlayer.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note: that select Java as the programming language.
Step 2: Add dependency to the build.gradle(Module:app)
Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.
implementation "androidx.media3:media3-exoplayer:1.3.1"
implementation "androidx.media3:media3-exoplayer-dash:1.3.1"
implementation "androidx.media3:media3-ui:1.3.1"
After adding this dependency sync the project.
Note: This is the latest version androidx.media3 all the previous versions are considered depreciated.
Step 3: Add internet permission in your Manifest file
Navigate to the app > manifest folder and write down the following permissions to it.
<!--Internet permission and network access permission-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Step 4: Working with the activity_main.xml
Now we will start implementing our ExoPlayerView in our XML file. Navigate to the app > res > layout > activity_main.xml. Inside that file add the below code.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:use_controller="true"
app:resize_mode="fit"/>
</RelativeLayout>
Step 5: Working with the MainActivity.java file
Navigate to the app > java > your apps package name > MainActivity.java file. Inside that file add the below code. Comments are added inside the code to understand the code in more detail.
MainActivity.java
package com.geeksforgeeks.exoplayer;
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;
public class MainActivity extends AppCompatActivity {
private ExoPlayer player;
private PlayerView playerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playerView = findViewById(R.id.player_view);
// Initialize ExoPlayer
player = new ExoPlayer.Builder(this).build();
playerView.setPlayer(player);
// Build the MediaItem
String videoUrl = "https://media.geeksforgeeks.org/wp-content/uploads/20201217163353/Screenrecorder-2020-12-17-16-32-03-350.mp4";
Uri uri = Uri.parse(videoUrl);
MediaItem mediaItem = MediaItem.fromUri(uri);
// Prepare the player with the media item
player.setMediaItem(mediaItem);
player.prepare();
player.setPlayWhenReady(true); // Start playing when ready
}
@Override
protected void onStop() {
super.onStop();
if (player != null) {
player.release();
player = null;
}
}
}
Note: We have used this video in this project.
Output:
Similar Reads
Animation in Android with Example Animation is the process of adding a motion effect to any view, image, or text. With the help of an animation, you can add motion or can change the shape of a specific view. Animation in Android is generally used to give your UI a rich look and feel. The animations are basically of three types as fo
7 min read
Image Switcher in Android with Example Sometimes, you may not want an image to appear suddenly on the screen. Instead, you might prefer a smooth transition from one image to another using animation. Android offers a tool called ImageSwitcher to help with this. An ImageSwitcher lets you add simple transition effects to your images.What ar
4 min read
Popup Menu in Android With Example In Android development, Menus are an important part of the user interface, providing users with easy access to common functionalities and ensuring a smooth and consistent experience throughout the application. In Android, we have three types of Menus available to define a set of options and actions
4 min read
Jetpack LiveData in Android with Example Android Jetpack is a suite of libraries to help developers follow best practices, reduce boilerplate code, and write code that works consistently across Android versions and devices so that developers can focus on the code they care about. Here, we are going to implement Jetpack Live Data in Android
4 min read
PhotoView in Android with Example In this article, PhotoView is added to android. PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView using multi-touch and double-tap. Besides that, it has many more features like it notifying the application when the user taps on the photo or when the displa
2 min read
Auto Image Slider in Android with Example Auto Image Slider is one of the most seen UI components in Android. This type of slider is mostly seen inside the apps of big E-commerce sites such as Flipkart, Amazon, and many more. Auto Image Slider is used to represent data in the form of slides that change after a specific interval of time. In
6 min read