Lets Dive into Exo-Player (Part I)

Prateek Batra
ProAndroidDev
Published in
5 min readSep 29, 2020

--

What is Exo-Player?

ExoPlayer is an application-level media player for Android. It’s an alternative to Android’s Original MediaPlayer for playing audio and video both locally and over the Internet along with support for many Adaptive & Progressive Streaming and support for subtitles, multiple audio support …etc

ExoPlayer Whole Media Support

Some Basic History & Overview (Back to where it all started)

Before Exo-Player came into existence there was an Android multimedia framework with which one can easily integrate & play audio or video from media files stored in your application’s resources (raw resources), from standalone files in the filesystem, or from a data stream arriving over a network connection, all using MediaPlayer APIs.

Exo-Player made an official release in Google I/O 2017
Since we are not getting here on deep you can watch the video

What is Progressive Streaming?

Progressive Streaming is a video streaming mechanism in which we basically have a single mp4 file that is streamed on every device and overall the different internet speed

What is Adaptive Streaming?

Adaptive Streaming is a video streaming mechanism in which whenever we play any resource it will automatically adapt itself on basis of resolution and streaming speed.

In adaptive streaming, there is different video size and quality based on screen resolution and internet speed while is not so in Progressive streaming.

Exo-Player — Implementation

Gradle Dependency

In your project module Gradle make sure you have :

allprojects {
repositories {
google()
jcenter()
}
}

In your app module Gradle add :

This is the main dependency for exo-player implementation but in case you want to use some of its specific features you can use as follow

Legacy Exoplayer Implementationimplementation 'com.google.android.exoplayer:exoplayer:2.X.X'For core functionalities(exoplayer-core:2.X.X)//Required
For DASH Support (exoplayer-dash:2.X.X)
For HLS Support (exoplayer-hls:2.X.X)
For Smooth Streaming Support (exoplayer-smoothstreaming:2.X.X)
For UI components and resources Support (exoplayer-ui:2.X.X)
Jetpack Media 3 Implementation*implementation "androidx.media3:media3-exoplayer:1.X.XFor UI components (media3-ui:1.X.X)//Required
For DASH Support (media3-exoplayer-dash:1.X.X)
For HLS Support (media3-exoplayer-hls:1.X.X)

We will use the library version as follow
Legacy Exoplayer: 2.18.5
Jetpack Media 3 : 1.0.0

Also, keep in mind to add this :
android {
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}

User Interface

Now coming to UI we will be using the

  • StyledPlayerView in Legacy Exoplayer Implementation
  • PlayerView in new Jetpack Media 3 Implementation

How to Setup/Initialise

First, we need to create, initialize & set our player with the media source we want along with the after based on our activity lifecycle we can release/reinitialize our player

Simple Exo-Player: This is a wrapper that is our player

Data Source Factory: This is a Factory data reader for our data source i.e which can be any file, asset, https…etc each of our media sources requires a data source factory

Media Source: This defines, provides & loads the media to be played by our Player. Now Based on our media type we have a different types of media sources like HLS Media Source, Progressive Media Source(earlier used Extractor Media Source), Smooth Streaming Media Source..etc.

Now Along the lifecycle of our Activity, we need to handle our player too as you must release those resources for other apps to use when we’re not using them, i.e when your app is put into the background.

  1. For API≥24 we have Multiwindows so there might be the case it might not be active in the split-window mode so you need to initialize the player in onStart and we will release our player onStop
  2. For API<24 we need to wait until our activity has been allocated resources and is visible so we will initialize the player in onResume or case it is not initialized and we will release our player onPause
  3. When our activity is destroyed we should release ur player so as not to cause any kind of memory leak

Now this is how our player look likes

Exo Player Player

What threads does this Exo Player work?

Well, we don’t need to worry about the thread but for the information,

Exo-Player is accessed from the main thread but for any of its internal working operations like buffering, preparing, or playing files for theses it has an internal playback thread that uses a classic thread and handler mechanism in which when we operate on our player (for example a seek) a message is delivered to the internal playback thread via a message queue and vice versa a message is delivered to our main thread on any playback event

What's Next ?

Some Additional Implementations to try

Some Additional thoughts?

With the new Styled UI, it has solved our many problems like
Now we don’t need to add speed control, Subtitle, or Audio manually now Also we have a very decent UI with much new functionality as compared to the previous one.

But still, there can be cases when we need to have a custom UI functionality along with that we need to add multiple video quality support

Do check out this awesome code lab by google:
https://codelabs.developers.google.com/codelabs/exoplayer-intro

I hope you will find this useful & if you liked this article, please consider giving it a thumbs up and sharing it with your friends. See you soon with the next one!

Catch me on Twitter: https://twitter.com/its_pra_tick

--

--