Adaptive streaming with Exoplayer

Let’s create one Adaptive exoplayer with playback speed, fast forward, backward options and track selection which shows video resolution option to play the video.

For adaptive streaming with exoplayer we need to create video with file format like .mpd or m3u8. To know more about m3u8 file click here

If you want to create your own video file with streaming options.
Follow below link.

How to create .mpd or .m3u8 video file from server using FFMPEG.

Exoplayer does not support mp4 file format for adaptive streaming but you can choose mp4 like format for auto streaming.Auto streaming is something which is completely depends on speed and connectivity of the internet.

Checkout the latest version of exoplayer from below link
https://github.com/google/ExoPlayer/releases

Track selection

MappingTrackSelector.MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
if (mappedTrackInfo != null) {
MappingTrackSelector.MappedTrackInfo.RENDERER_SUPPORT_NO_TRACKS);
Pair<AlertDialog, TrackSelectionView> dialogPair =
TrackSelectionView.getDialog(this, "Select Video Resolution", trackSelector, 0);
dialogPair.second.setShowDisableOption(false);
dialogPair.second.setAllowAdaptiveSelections(false);
dialogPair.first.show();

Player initialisation



TrackSelection.Factory trackSelectionFactory;
trackSelectionFactory = new AdaptiveTrackSelection.Factory();


@DefaultRenderersFactory.ExtensionRendererMode int extensionRendererMode =
((App) getApplication()).useExtensionRenderers()
? (true ? DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_ON)
: DefaultRenderersFactory.EXTENSION_RENDERER_MODE_OFF;

DefaultRenderersFactory renderersFactory = new DefaultRenderersFactory(this, null,
DefaultRenderersFactory.EXTENSION_RENDERER_MODE_PREFER);

trackSelector = new DefaultTrackSelector(trackSelectionFactory);
trackSelector.setParameters(trackSelectorParameters);
lastSeenTrackGroupArray = null;


DefaultAllocator defaultAllocator = new DefaultAllocator(true, C.DEFAULT_VIDEO_BUFFER_SIZE);
DefaultLoadControl defaultLoadControl = new DefaultLoadControl(defaultAllocator,
DefaultLoadControl.DEFAULT_MIN_BUFFER_MS,
DefaultLoadControl.DEFAULT_MAX_BUFFER_MS,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS,
DefaultLoadControl.DEFAULT_TARGET_BUFFER_BYTES,
DefaultLoadControl.DEFAULT_PRIORITIZE_TIME_OVER_SIZE_THRESHOLDS
);

player = ExoPlayerFactory.newSimpleInstance(/* context= */ MainActivity.this, renderersFactory, trackSelector, defaultLoadControl);
player.addListener(new PlayerEventListener());
player.setPlayWhenReady(startAutoPlay);
player.addAnalyticsListener(new EventLogger(trackSelector));
playerView.setPlayer(player);
playerView.setPlaybackPreparer(this);

mediaSource = buildMediaSource(Uri.parse("http://13.127.14.184/adaptive/lecturevideo/sample_lecture_playlist.m3u8"));

player.prepare(mediaSource);

Build Media source based on different different kinds of URI

private MediaSource buildMediaSource(Uri uri, @Nullable String overrideExtension) {
@C.ContentType int type = Util.inferContentType(uri, overrideExtension);
switch (type) {
case C.TYPE_DASH:
return new DashMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
case C.TYPE_SS:
return new SsMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
case C.TYPE_HLS:
return new HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
case C.TYPE_OTHER:
return new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
default: {
throw new IllegalStateException("Unsupported type: " + type);
}
}
}

Github repository link

https://github.com/MayurSolanki/AdaptiveStreamingExoplayer

Check out the latest code from above link and just replace .m3u8 link by your own m3u8 link.

video link

https://youtu.be/i-GiZvJzRv0

For more Resources on Exoplayer follow below link.

Check out my new article on adaptive download using Exoplayer

Android Developer | AndroidThings | IOT | MQTT | AWS | Flutter