Exoplayer Integration Android

October 22, 2021 Publish By : EXPERT APP DEVS 5 min read Viewed By : 182
exoplayer android

Before the Exoplayer, Native Mediaplayer was the only solution for playing video and audio on an android device locally or over the internet. In the beginning, playing a simple video using Native MediaPlayer was easy but if you wanted to develop a player like Youtube it would be very hard using MediaPlayer and it would take a lot of time and also you need to put in a lot of effort and you can face a lot of challenges as well.

ExoPlayer is the video player currently used in the Android YouTube app. It’s not a part of android native it’s a library that was developed by Google and maintained by Google. Using the ExoPlayer we can manage all audio and video-related stuff on our behalf and we can use a few methods and classes for developing such a good player. We can customize the player UI as well using Exoplayer.

ExoPlayer was built and developed using Android MediaCodec API, which was released in Android 4.1 (API level 16 - Jelly bean). So, if we want to use Exoplayer in our app we need to set minimum API version 16 for our apps.

Why use ExoPlayer?

  • We can stream DASH (Dynamic Adaptive Streaming over HTTP), HLS(HTTP Live Streaming), SmoothStreaming. and also perform Common Encryption, using Exoplayer which is not supported by MediaPlayer.
  • We can easily modify and customize our player design.
  • Exoplayer has vast features and more flexibility so most of the apps use the exo-player for streaming video.
  • Good Explanation in documentation and tutorials.
  • It’s an open-source project so it’s totally free for all!

Basic Android ExoPlayer App

In your gradle files, add these lines:

//project's build.gradle
repositories {

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8

dependencies {
    implementation 'com.google.android.exoplayer:exoplayer:2.10.5' //current version 


Streaming and Play a video via Exoplayer

The procedure or steps for playing a video via Exoplayer are as per below:

  • add the <player_view> in XML and create its instance: This will be the view on which the video will be rendered.
  • create a Player instance: player instance role in streaming, decoding, and rendering of bitstreams of your video file.
  • create a MediaSource Instance: This is the Class working with the Uri of the video and audio content you added to play in the player.
  • attach the player view to player
  • start video playback via exo-player.setPlayWhenReady(true);:  it will automatically start playing video when it is available and ready to play. (If you want to control manually then pass the false. it’s played when the user presses the play button.)

In your activity or framgment's xml file, add these code snippet:


In your activity or fragment's java file, add below lines of code:

SimpleExoPlayer exoplayer; 
PlayerView player view;

   protected void onCreate(Bundle savedInstanceState) {
      String VIDEOURL= "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4";
      int appName = R.string.app_name;

       playerView = findViewById(R.id.pv_main);  // for creating a player view
      TrackSelector trackSelectorDef = new DefaultTrackSelector();
      exoplayer= ExoPlayerFactory.newSimpleInstance(this, trackSelectorDef); 

// creation a player instance
      String userAgent = Util.getUserAgent(this, this.getString(appName));
      DefaultDataSourceFactory defdataSourceFactory = new DefaultDataSourceFactory(this,userAgent);
      Uri uriOfContentUrl = Uri.parse(VIDEOURL);
      MediaSource mediaSource = new ProgressiveMediaSource.Factory(defdataSourceFactory).createMediaSource(uriOfContentUrl);  
// creating a media source

      exoplayer.setPlayWhenReady(true);  // start loading video and it’s play when video ready to play
       playerView.setPlayer(exoplayer); // attach surface to the xml file view

For Pausing Video

private void pausePlayer(SimpleExoPlayer player) {
      if (player != null) {

For resuming the video:

private void playPlayer(SimpleExoPlayer player) {
      if (player != null) {

For stopping the video:

private void stopPlayer(){
      exoplayer= null;

For seeking the video to some point:

private void seekTo(SimpleExoPlayer player, long positionInMS) {
      if (player != null) {


That’s all, for now. I hope you learned something new. We will cover advanced state handling, Live stream handling, Customization of UI similar to Other Apps, etc in my upcoming blog. Stay tuned with a custom android app development company - Expert App Devs!

Need a consultation?

Drop us a line! We are here to answer your questions 24/7.