ExoPlayer – Add simple audio player to Android

If you have ever had to develop an Android app that played audio or video, you must have heard of MediaPlayer, a quick solution provided by Android framework for playing media.

Yes, it is simple to use. With only a few lines of code you can reproduce basic audio or video. But what if you need more features?

ExoPlayer

ExoPlayer is an open sourced media player built on Android’s low level media APIs. It’s designed to be easy to customize and extend, allowing many components to be replaced with custom implementations.

Advantages

ExoPlayer has a number of advantages over Android’s built in MediaPlayer:

  • Support for Dynamic Adaptive Streaming over HTTP (DASH) and SmoothStreaming, neither of which are are supported by MediaPlayer (it also supports HTTP Live Streaming (HLS), MP4, MP3, WebM, M4A, MPEG-TS and AAC).
  • Support for advanced HLS features, such as correct handling of #EXT-X-DISCONTINUITY tags.
  • The ability to customize and extend the player to suit your use case. ExoPlayer is designed specifically with this in mind, and allows many components to be replaced with custom implementations.
  • Easily update the player along with your application. Because ExoPlayer is a library that you include in your application apk, you have control over which version you use and you can easily update to a newer version as part of a regular application update.
  • Fewer device specific issues.

Adding ExoPlayer to an Android app

Because ExoPlayer is a library that you include in your application, it can be easily updated along with your app.

Note: ExoPlayer’s standard audio and video components rely on Android’s MediaCodec API, which was released in Android 4.1 (API level 16). Hence they do not work on earlier versions of Android.

Add ExoPlayer to Android Studio

The easiest way to get started using ExoPlayer is by including the following in your project’s build.gradle file:

compile 'com.google.android.exoplayer:exoplayer:r1.5.3'

I’m using release 1.5.3 but you can look at the release history to choose the one you want.

Creating an instance of the ExoPlayer:


private ExoPlayer exoPlayer;
...
@Override
public void onCreate() {
exoPlayer = ExoPlayer.Factory.newInstance(1);
}

Where 1 is the number of renderers to be used. In this case it’s 1 since we will only use an audio renderer. If you need audio and video renderers it should be 2.

Preparing the exoPlayer:

A TrackRenderer plays a specific type of media, such as video, audio or text. The ExoPlayer class invokes methods on its TrackRenderer instances from a single playback thread, and by doing so causes each type of media be rendered as the global playback position is advanced.

The ExoPlayer library provides MediaCodecAudioTrackRenderer for audio. This implementation makes use of Android’s MediaCodec class to decode individual media samples. They can handle all audio and video formats supported by a given Android device.


private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
private static final int BUFFER_SEGMENT_COUNT = 256;
...
// String with the url of the radio you want to play
String url = getRadioUrl();
Uri radioUri = Uri.parse(url);
// Settings for exoPlayer
Allocator allocator = new DefaultAllocator(BUFFER_SEGMENT_SIZE);
String userAgent = Util.getUserAgent(context, "ExoPlayerDemo");
DataSource dataSource = new DefaultUriDataSource(context, null, userAgent);
ExtractorSampleSource sampleSource = new ExtractorSampleSource(
radioUri, dataSource, allocator, BUFFER_SEGMENT_SIZE * BUFFER_SEGMENT_COUNT);
audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
// Prepare ExoPlayer
exoPlayer.prepare(audioRenderer);

 

The Exo Player library provides ExtractorSampleSource to play traditional media formats, including MP3, M4A, MP4, WebM, MPEG-TS and AAC.

Play and Stop Exo Player:

When all settings are ready, you must call setPlayWhenReady(true) method.
When you are done using it, call exoPlayer.stop() and don’t forget to release with exoPlayer.release()

Add ons:

Checking state:

In order to check the current state of the player we can use getPlayWhenReady() method.


if (exoPlayer != null && exoPlayer.getPlayWhenReady()) {
... do something when exo player is playing...
}

Muting/Unmuting Player:


public static void Mute() {
exoPlayer.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, 0f);
}

public static void Play() {
exoPlayer.sendMessage(audioRenderer, MediaCodecAudioTrackRenderer.MSG_SET_VOLUME, 1f);
}

More add ons:

For more features for reproducing audio or video you can visit the ExoPlayer developer guide:

https://google.github.io/ExoPlayer/guide.html

Other resources:

For more information about this library you can take a look at the Google IO video of one of Google’s software engineers Oliver Woodman.