Adaptive HLS streaming on android with Google’s ExoPlayer

Streaming video and audio using the default media player API of Android can be a pain when it comes to adaptive streaming and customization. Google’s ExoPlayer is an application level media player which provides consistent API and easy customization for playing videos both locally and over the internet. ExoPlayer supports features not currently supported by Android’s MediaPlayer API, including DASH, HLS and SmoothStreaming adaptive playbacks.

Here we’ll look into integrating ExoPlayer into an android application for streaming video using HLS protocol. HLS stands for HTTP Live Streaming which resembles MPEG-DASH in that it works by breaking the overall stream into a sequence of small HTTP-based file downloads, each download loading one short chunk of an overall potentially unbounded transport stream. HLS protocol is widely used by Vimeo, Apple and other companies which provides live streaming of videos.

Let’s get started

Create a new android studio project and add make sure you have JCenter and Google repositories included in the project level build.gradle file.

Add these dependencies into your app level build.gradle file to include ExoPlayer libraries for HLS streaming.

Additionally if you want to use DASH or Smooth Streaming instead of HLS use the following dash module instead of hls

Customizing layout for ExoPlayer

Whether you are using an Activity, Fragment or Dialog. Insert this into the layout file of the component. Here I’ll be demonstrating on an Activity’s layout file.

Additionally, I’ve added a progress indicator to display during initial load and buffering. In the line #14 I have included a custom layout for Exo controller. You can avoid this line if you don’t want custom control customization. We need to define a layout file for customizing and give ids to the element as specified in the original Exo layout. (Hopefully, the android studio Intellisense will help you in that).

You can specify additional TextViews in the layout for displaying current position and end time of the video with ids exo_position and exo_duration

Initializing and Playing the Video

In the Activity declare the PlayerView we added in the layout as private PlayerView playerView and initialize it inside the onCreate method with the id of the view.

Additionaly we need a few variables initialized to be used to save the player state on orientation changes and activity pause.

we need to initialize the ExoPlayer with the streaming URL and configurations in the onStart method of the activity.

Cleaning Up!

We need to release the player when the activity goes to the background or is paused. Create the following method and call it inside the onStop() and onPause() method of your activity.

A customized ExoPlayer with auto rotate and close buttons on toolbar.
That’s it! Your video will be playing now! For additional information on playing DASH videos visit the Google CodeLabs, or for the documentation of the ExoPlayer visit the GitHub repository. Feel free to ask any queries in the comment section below.

Also published on Medium.


Community Lead with a demonstrated history of working in a non-profit organization. Skilled in Python, Public Speaking, Graphic Design, Writing, and Android Development. Strong community and social services professional with a Bachelor's degree focused in Computer Science from Sahyadri College of Engineering and Management.