Working of ExoPlayer in Android
Posted By : Akash Tomer | 19-Dec-2017
ExoPlayer is a low-level media player for Android devices, and it provides the method to play any type of media. It is an open source third party library made by Google. It is written in Java and it is depended on low-level media encoding APIs. It is far
Pros
- It supports DASH over HTTP and SmoothStreaming which are not supported by MediaPlayer.
- Its supports DRM(Digital Right Management) technology.
- It supports a variety of media formats which are MP4, MP3, WebM, M4A, MPEG-TS, and AAC.
- It provides advanced HLS features eg #EXT-X-DISCONTINUITY tags
- It is a lightweight library and easily integrates with a number of a libraries using official extensions eg IMA.
- It supports the Widevine common encryption technology on Android 4.4 (API level 19) and higher.
Cons
- It relies on Android media codec for android devices.And media codec is released in API level 16.Hence do not work for the earlier version.
Getting started
First, we have to add the dependency to our grade.This dependency includes the full ExoPlayer Library.
compile 'com.google.android.exoplayer:exoplayer:2.5.2'
If you want to add the specific module then you just need to add
compile 'com.google.android.exoplayer:exoplayer-core:2.5.2'
compile 'com.google.android.exoplayer:exoplayer-dash:2.5.2'
compile 'com.google.android.exoplayer:exoplayer-ui:2.5.2'
To play any media in Exo player we have to work on it player UI component and the core part. The player that is controlled by a user to perform the big actions (i.e. play, , pause, stop, next), and the internal operation parts that fetch the stream, decode it, and process it for playing.For creating the instance of player you have to add the below line of code.
DefaultBandwidthMeter BANDWIDTH = new DefaultBandwidthMeter();
TrackSelection.Factory adaptiveTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(BANDWIDTH);
defaultTrackSelector = new DefaultTrackSelector(adaptiveTrackSelectionFactory);
trackSelectionHelper = new TrackSelectionHelper(defaultTrackSelector, adaptiveTrackSelectionFactory);
eventLogger = new EventLogger(trackSelector);
exoplayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
Now you have to add the view to the player.First, in your layout XML add these line
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/exoplayer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:focusable="true"
app:resize_mode="fill" />
And then in your java file
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);
simpleExoPlayerView.setPlayer(player);
In
private MediaSource buildMediaSource(Uri uri) {
int type = Util.inferContentType(uri);
switch (type) {
case C.TYPE_SS:
return new SsMediaSource(uri, buildDataSourceFactory(false),
new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
case C.TYPE_DASH:
return new DashMediaSource(uri, buildDataSourceFactory(false),
new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler, eventLogger);
case C.TYPE_HLS:
return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, eventLogger);
case C.TYPE_OTHER:
return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(),
mainHandler, eventLogger);
default: {
throw new IllegalStateException("Unsupported type: " + type);
}
}
}
If you want to handle it callback you just need to implement the EventListner.
@Override public void onLoadingChanged(boolean isLoading) { ; // when the media is loiading. }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState)
{
if (playWhenReady)
{
if (playbackState == Player.STATE_BUFFERING)
{ }
}
if (playbackState == Player.STATE_READY) { }
if (playbackState == Player.STATE_ENDED) { }
}
If you want to make a media source list so that it can play one by one or you want to repeat a particular media than you should use ConcatenatingMediaSource.
MediaSource[] mediaSources = new MediaSource[uris.length];
for (int i = 0; i < uris.length; i++) {
mediaSources[i] = buildMediaSource(uris[i]);
}
MediaSource mediaSource = mediaSources.length == 1 ? mediaSources[0]
: new ConcatenatingMediaSource(mediaSources);
Hope this will help you.
Best of luck.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akash Tomer
Akash is an Android Developer at Oodles Technology.