Working with ExoPlayer — The clean way — and customization

Prashant Barahi
May 17, 2019 · 4 min read
Image for post
Image for post
Photo by Maximilian Weisbecker on Unsplash

ExoPlayer is a open-source super customizable audio/video player from Google. It is able to play DASH (Dynamic Adaptive Streaming over HTTP), HLS (HTTP Live Streaming) and SmoothStreaming; merge, loop, concatenate, cache medias — while allowing developer to define custom load control, define seamless transition between successive medias and much more. So without further delay, lets get started.

Image for post
Image for post

Installation

repositories {
google()
jcenter()
}
implementation 'com.google.android.exoplayer:exoplayer:2.X.X'

P.S For cleaner code, we will be using Data binding Library and BindingAdapter so do not forget to enable these in gradle.

UI setup

Usage

RecyclerView’s Adapter code implementing the PlayerStateCallback

P.S- Here’s a helpful gist containing a number of streaming URLs you can use to test your implementation.

Notice how we are setting the stream URL for the video in url variable available from the data binding library. The app:video_url and the app:on_state_change part in the xml file is from BindingAdapter. The callback is used to communicate the state of the video player like buffering, paused, playing or idle so that we can handle these change of state in our main code. Now for the fun part — Lets create an extension function with @BindingAdapter annotation, that we will use to setup our PlayerView, with the values we have passed in our xml file as the arguments.

Use the binding we have passed in the xml file

You can also pass in the view for ProgressBar as an argument to above extension function.

Great! Look at how clean the RecyclerView.ViewAdapter code is! There’s barely anything there. We have successfully started streaming the videos from the test URLs.

Now for the advance part!

Caching

Note: I have used a local Context here, but in singleton dependencies, you should use the application context. It’s better to inject it.

And to use this cache as data source, instead of DefaultHttpDataSoruceFactory — use the above created LocalCacheDataSourceFactory.

Image for post
Image for post
Using local cache to playback media

Seamless transition between medias

Image for post
Image for post
Using Media source composition

Load Control

Image for post
Image for post

MindOrks

Our community publishes stories worth reading on Android…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store