What is Exoplayer and How to Implement it in Android

Raja Pratap Mar 2nd, 2023

Image Source

Exoplayer is one of the most highly recommended media player for streaming videos on Android. Even Google apps like YouTube and Google TV use it. Exoplayer makes it simple to stream videos securely on Android, despite the fact that it is a cumbersome task. Exoplayer’s extensive customization features makes it suitable for application in a wide range of scenarios. Wide range of media formats are supported by Exoplayer, including adaptive streaming format such as HLS, Dash, and Smooth Streaming. With its widevine support, you can be assured that your content is secure.

For video streaming giants like YouTube, Netflix and more, ExoPlayerView is currently one of the most used UI components.It is utilised in Android apps for both audio and video streaming. ExoPlayer is a media player library which provides a way to play audio and video with lots of customization in it. Moreover, ExoPlayer is a library considered as the best alternative source for playing audio and videos on Android. This library will help you to customize your media player according to the requirements.

Advantages of Using ExoPlayer: –

  • With the help of ExoPlayer, you can directly fetch media files such as audios and videos from the internet and play them inside the ExoPlayer.
  • It offers smooth encryption and streaming of video and audio files.
  • ExoPlayer provides you the ability to customize your media player according to your requirements.
  • You can clip or merge your media easily with the support of Exoplayer

Step-by-Step Implementation of ExoPlayer in Android: –

Here, you will know about steps involved in creating a simple video player app by fetching a video from a URL and play that video inside our ExoPlayer.

Note: We are using JAVA here for implementing ExoPlayer in Android.

Step 1: Create a New Project

To create a new project in Android Studio, start by “Create/Start a New Project”. You should select Java as the programming language.

Step 2: Add dependency to the build.gradle(Module:app)

video content management system

After adding this dependency, you need to sync this Project.

Step 3: Add internet permission in your Manifest file

As given below, you need to Navigate to the app -> manifest folder and write down the permissions to it.

educational streaming service

Step 4: Working with the activity_main.xml

Now, we will implement our ExoPlayerView in XML file. Navigate to the app > res > layout > activity_main.xml. Inside the same file add the below code.

video streaming solution

Step 5: Working with the MainActivity.java file

Navigate to the app > java > your apps package name > MainActivity.java file.
Here, inside that file add the below code. Comments are also added inside the code to better understand the code.

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 = “sample-url”;

protected void onCreate(Bundle savedInstanceState) {
exoPlayerView = findViewById(R.id.idExoPlayerVIew);
try {

// BandwidthMeter 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

// we are preparing our exoplayer
// with media source.

// we are setting our exoplayer
// when it is ready.

} catch (Exception e) {
// below line is used for
// handling our errors.
Log.e(“TAG”, “Error : ” + e.toString());

Step 5: Handling the player controls Methods

  • play and pause: Used to play and pause the video
  • seekTo: This seeks to a position which is specified in milliseconds in the current MediaItem
  • playWhenReady: Whether playback should proceed when ready
  • hasNextMediaItem, hasPreviousMediaItem, seekToPreviousMediaItem, seekToNextMediaItem: Allows navigating through the playlist
  • setPlaybackParameters: Used to set the playback parameters. The playback parameters changes may cause the player to buffer.
  • Player.Listener.onPlaybackParametersChanged(PlaybackParameters) will be called whenever the currently active playback parameters change

How to Customize Exoplayer?

ExoPlayer has many customizations available from UI adjusting, deciding caching mechanism for data loaded from the network to customizing server interaction to check HTTP requests and responses, customizing error handling policy, allowing asynchronous buffer queueing, and a lot more.

In this section, we will see how we can customize the UI with ExoPlayer.

Customizing ExoPlayer’s UI components

ExoPlayer V2 includes several out-of-the-box UI components for customization, most notably:

  • SimpleExoPlayerView is a high-level view for media playbacks. It displays video, subtitles, and album art during playback, and playback controls using a PlaybackControlView.
  • PlaybackControlView is a view for controlling ExoPlayer instances. It displays a standard playback controls, such as a play/pause button, fast-forward and rewind buttons, and a seek bar.

Setting attributes for SimpleExoPlayerView

A SimpleExoPlayerView can be included in the layout for an Activity belonging to a video application as follows-

Now you can use the following attributes on SimpleExoPlayerView to customize it when used in a layout XML file.

  • use_artwork – Whether the artwork is used if available in audio streams.
  • default_artwork – Default artwork to use if no artwork is available in audio streams.
  • use_controller – Whether the playback controls can be shown.
  • hide_on_touch – Whether the playback controls are hidden by touch events.
  • auto_show – Whether the playback controls are automatically shown when playback starts, pauses, ends or fails. If set to false, the playback controls can also be manually operated with showController() and hideController().
  • resize_mode – Controls how video and album art is resized within the view. Valid values are fit, fixed_width, fixed_height, and fill.
  • surface_type – The type of surface view used for video playbacks. Valid values are surface_view, texture_view, and none. Using none is recommended for audio-only applications since creating the surface can be expensive. Using
  • surface_view is recommended for video applications.
  • shutter_background_color – The background color of the exo_shutter view.
  • player_layout_id – Specifies the id of the layout to be inflated.
  • controller_layout_id – Specifies the id of the layout resource to be inflated by the child PlaybackControlView.

Specifying a custom layout file

Defining your own exo_simple_player_view.xml is very much useful to customize the layout of SimpleExoPlayerView in your application. You can also customize the layout for a single instance in a layout file. This can be achieved by setting the player_layout_id attribute on a SimpleExoPlayerView. This makes the specified layout inflated instead of exo_simple_player_view.xml, i.e. only the instance on which the attribute is set.

For more customization options, check the official document on customizing ExoPlayer.

Adaptive Bitrate Streaming in Exoplayer

Exoplayer is a best option for adaptive bitrate streaming to set video quality automatically based on network bandwidth. Adaptive bitrate streaming is a technology primarily designed to deliver video in the best way possible and with high quality for each user and device.

When the connections are slow, the video quality will be low, and for fast connections, the video quality will be best with less buffer time. These qualities (including bit rates and resolutions) are termed as tracks. The same piece of media is divided into several tracks, each with a different quality determined by the bit rate and resolution. Each track is divided into segments that range in length from 2 to 10 seconds. With varying network speeds and signals, this makes switching between tracks simpler.

Implementing Adaptive Track Selection

To implement adaptive streaming, add a TrackSelector while initializing the player initialization. TrackSelector is mainly used to switch between multiple tracks.

As given, you need to Create an adaptive track selection factory with default parameters and pass it to DefaultTrackSelector, i.e. used for choosing tracks in the media item. Then pass the trackSelector to the ExoPlayer builder.

The blog covers all the pointers on implementing Exoplayer in Android Studio. Drop a query to us if you have any questions regarding ExoPlayer and its usage. You can also checkout the ExoPlayer official website available to read the official documentation, release notes and more.

Raja Pratap- Android Developer
Raja is an experienced Android Developer with a demonstrated history of working in the computer software industry. Skilled in OTT, SQLite,Kotlin, Core Java, Web Services API.

Add a Comment

Your email address will not be published. Required fields are marked *

Looking for Streaming Solutions?

  • Go-Live on 12+ platforms
  • Zero Revenue Share
  • Multiple Monetization Models
  • 50+ Integrations

get a demo today

Take control of your digital media strategy.
Contact us for a no-obligation demo of the Experience
Cloud, tailor-made for you!

    By Continuing, you agree to our Terms & Conditions and Privacy Policy