ExoPlayer in Android with Example - GeeksforGeeks

15 Dec, 2021
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. 
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
MediaPlayer
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. Note that we are using JAVA for implementing ExoPlayer in Android. 
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. 
// dependency for exoplayer
implementation ‘com.google.android.exoplayer:exoplayer:r2.4.0’
// for core support in exoplayer.
implementation ‘com.google.android.exoplayer:exoplayer-core:r2.4.0’
// for adding dash support in our exoplayer.
implementation ‘com.google.android.exoplayer:exoplayer-dash:r2.4.0’
// for adding hls support in exoplayer.
implementation ‘com.google.android.exoplayer:exoplayer-hls:r2.4.0’
// for smooth streaming of video in our exoplayer.
implementation ‘com.google.android.exoplayer:exoplayer-smoothstreaming:r2.4.0’
// for generating default ui of exoplayer
implementation ‘com.google.android.exoplayer:exoplayer-ui:r2.4.0’
After adding this dependency sync the project.
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.
XML
<?xml version="1.0" encoding="utf-8"?><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">      <!--Widget for exoplayer view-->    <com.google.android.exoplayer2.ui.SimpleExoPlayerView        android:id="@+id/idExoPlayerVIew"        android:layout_width="match_parent"        android:layout_height="500dp" />      </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.
Java
import android.net.Uri;import android.os.Bundle;import android.util.Log;  import androidx.appcompat.app.AppCompatActivity;  import com.google.android.exoplayer2.ExoPlayerFactory;import com.google.android.exoplayer2.SimpleExoPlayer;import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;import com.google.android.exoplayer2.extractor.ExtractorsFactory;import com.google.android.exoplayer2.source.ExtractorMediaSource;import com.google.android.exoplayer2.source.MediaSource;import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;import com.google.android.exoplayer2.trackselection.TrackSelector;import com.google.android.exoplayer2.ui.SimpleExoPlayerView;import com.google.android.exoplayer2.upstream.BandwidthMeter;import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;  public class MainActivity extends AppCompatActivity {      // creating a variable for exoplayerview.    SimpleExoPlayerView exoPlayerView;          // creating a variable for exoplayer    SimpleExoPlayer exoPlayer;          // url of video which we are loading.    String videoURL = "https://media.geeksforgeeks.org/wp-content/uploads/20201217163353/Screenrecorder-2020-12-17-16-32-03-350.mp4";      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        exoPlayerView = findViewById(R.id.idExoPlayerVIew);        try {                          // bandwisthmeter is used for             // getting default bandwidth            BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();                          // track selector is used to navigate between             // video using a default seekbar.            TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));                          // we are adding our track selector to exoplayer.            exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);                          // we are parsing a video url            // and parsing its video uri.            Uri videouri = Uri.parse(videoURL);                          // we are creating a variable for datasource factory            // and setting its user agent as 'exoplayer_view'            DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");                          // we are creating a variable for extractor factory            // and setting it to default extractor factory.            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();                          // we are creating a media source with above variables             // and passing our event handler as null,            MediaSource mediaSource = new ExtractorMediaSource(videouri, dataSourceFactory, extractorsFactory, null, null);                          // inside our exoplayer view             // we are setting our player            exoPlayerView.setPlayer(exoPlayer);                          // we are preparing our exoplayer            // with media source.            exoPlayer.prepare(mediaSource);                          // we are setting our exoplayer            // when it is ready.            exoPlayer.setPlayWhenReady(true);                    } catch (Exception e) {            // below line is used for             // handling our errors.            Log.e("TAG", "Error : " + e.toString());        }    }}
Note: We have used this video in this project.
Check out the project: https://github.com/ChaitanyaMunje/QRCodeGenerator/tree/ExoPlayer
kalrap615
android
Android-View
Technical Scripter 2020
Android
Java
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Broadcast Receiver in Android With Example
Resource Raw Folder in Android Studio
Android RecyclerView in Kotlin
CardView in Android With Example
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Stream In Java