Android ExoPlayer: Starter’s Guide

Yusuf Çakmak
AndroidPub
Published in
4 min readSep 18, 2017

My Story with ExoPlayer

I have been developing many media applications at my previous company. These applications include more than 30 radio stations and 20 TV channels.

Each app has its own problem. I have to figure out different solutions for each one by using different media player solutions. Among them were vitamio, vlc, googlemediaframework and a few more. I started exploring and using ExoPlayer with GoogleMediaFramework.

What is ExoPlayer & Why ?

ExoPlayer is an open source project that is not part of the Android framework and is distributed separately from the Android SDK. ExoPlayer’s standard audio and video components are built on Android’s MediaCodec API, which was released in Android 4.1 (API level 16). Because ExoPlayer is a library, you can easily take advantage of new features as they become available by updating your app.

ExoPlayer is very successful in terms of performance. It’s fast and stable. The biggest problem with the Exoplayer in the past was that the development process was not regular and there was no documentation. With the 2.x.x versions, many resources have been published in google-exoplayer publication.

Google developed ExoPlayer for media-centric applications like YouTube and Play Movies and then turned it on to community. Being used in large applications is the most important plus for me.

I have a simple example for ExoPlayer. I will continue to add some examples about features of ExoPlayer

How to Use ?

ExoPlayer switched to the modular structure with version 2.4. In this way, your application will be smaller and you won’t need some modules which is you don’t use.

compile 'com.google.android.exoplayer:exoplayer-core:r2.X.X'
compile 'com.google.android.exoplayer:exoplayer-dash:r2.X.X'
compile 'com.google.android.exoplayer:exoplayer-ui:r2.X.X'
compile 'com.google.android.exoplayer:exoplayer-smoothstreaming:r2.X.X'
compile 'com.google.android.exoplayer:exoplayer-hls'

If you don’t want to use module dependencies you can use all of them with one dependency.

compile 'com.google.android.exoplayer:exoplayer:r2.X.X'

So we add the ExoPlayer to our project, now we can start to play audio or video. But before we start, we have to be careful about source. Because if we use wrong source, media won’t be played. We need different MediaSource classes for different types.

HLS -> HlsMediaSource
DASH -> DashMediaSource
SS -> SsMediaSource
MP4 and others -> ExtractorMediaSource

Also you can play audio files with ExtractorMediaSource.

dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "mediaPlayerSample"), defaultBandwidthMeter);

mediaSource = new ExtractorMediaSource(Uri.parse("http://rs1.radiostreamer.com:8030"), dataSourceFactory, extractorsFactory, null, null);

player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

player.prepare(mediaSource);

player.setPlayWhenReady(true);

RadioPlayerActivity.java

Basically, we want to play video, we just define the followings.

DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

MediaSource mediaSource = new ExtractorMediaSource(Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"),
mediaDataSourceFactory, extractorsFactory, null, null);

player.prepare(mediaSource);

player.setPlayWhenReady(shouldAutoPlay);

VideoPlayerActivity.java

TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

simpleExoPlayerView.setPlayer(player);

player.setPlayWhenReady(shouldAutoPlay);

DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();

MediaSource mediaSource = new ExtractorMediaSource(Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"),
mediaDataSourceFactory, extractorsFactory, null, null);
player.prepare(mediaSource);

Custom UI

You can build custom players by overriding layout files. You need to create to files in your layout folder. Necessary files are given below. But first we can look how to do it. There it is two layout files enough for the building a custom player. This is an easy and good way for developers. Thanks for this, ExoPlayer Team :)

exo_playback_control_view.xml
exo_simple_player_view.xml

For example, we can add one more button for hiding control bar.

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="4dp"
android:orientation="horizontal">


<ImageView
android:layout_width="wrap_content"
android:src="@drawable/ic_hide"
android:id="@+id/exo_controller"
android:layout_height="wrap_content" />

<ImageButton android:id="@id/exo_play"
style="@style/ExoMediaButton.Play"/>

<ImageButton android:id="@id/exo_pause"
style="@style/ExoMediaButton.Pause"/>


</LinearLayout>

after adding view, we can add our code.

ivHideControllerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
simpleExoPlayerView.hideController();
}
});

Tips

I will share those tips below.

Playing lists: sometimes we need to play files as a list. You can use ConcatenatingMediaSource to play lists.

MediaSource[] mediaSources = new MediaSource[videos.size()];
for (int i = 0; i < videos.size(); i++) {
mediaSources[i] = buildMediaSource(Uri.parse(videos.get(i).getVideoPath()));
}
MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
: new ConcatenatingMediaSource(mediaSources);
player.seekTo(position,0);

Subtitles, if you want to add subtitles to your videos, you can use MergingMediaSource.

MediaSource mediaSource = new ExtractorMediaSource(Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"),
mediaDataSourceFactory, extractorsFactory, null, null);
Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
null, Format.NO_VALUE, Format.NO_VALUE, "en", null);
Uri uri = Uri.parse("http://www.storiesinflight.com/js_videosub/jellies.srt");
MediaSource subtitleSource = new SingleSampleMediaSource(uri, mediaDataSourceFactory, textFormat, C.TIME_UNSET);

MergingMediaSource mergedSource = new MergingMediaSource(mediaSource, subtitleSource);

player.prepare(mergedSource);

Supported formats

Conclusion

Exoplayer is the best solution when you develop media applications. Thanks for reading, If you think anything is missing in this article, please don’t hesitate and comment. I will update the article.

Resources

--

--