In the previous blog, we have discussed the basics of Android ExoPlayer, advantages & disadvantages. You can find its link here Introduction
to Android ExoPlayer.
In this blog, we are going to learn how to play live stream using ExoPlayer. We will be using HLS (HTTP Live Streaming) technology as communication protocol to serve the multimedia content. HLS is an adaptive streaming communications protocol. At first lets
discuss adaptive streaming since it is used in HLS.
Adaptive streaming provides the multiple bit-rate streams to end users. Best stream get played at user’s end, which is decided by client player on the basis of some parameters like client computational capacity (CPU), internet bandwidth and memory utilization.
Adaptive streaming provides the best user experience.
The Index File .m3u8 contains the index of all different version streams and manages the continuity and consistency of streams during playback. Hence for adaptive streaming it choses the files from low,
medium or high resolution w.r.t. internet connection and indexes of those files are provided by .m3u8 file. This is the brief introduction of HLS, now jump directly to
the implementation of ExoPlayer.
ExoPlayer supports HLS adaptive playbacks through use of HlsSampleSource. ThisHlsSampleSource loads chunks of media data and from that chunk of data individual samples are extracted.
HlsSampleSource is a SampleSource for HLS streams. This class contains various constructors & methods which are very effective in media playback. Some of them are listed below-
long playbackPositionUs)Indicates to the source that it should still be buffering data for the specified track.
(int track)Disable the specified track.
long positionUs)Enable the specified track.
()Returns an estimate of the position up to which data is buffered.
(int track)Returns the format of the specified track.
()Returns the number of tracks exposed by the source.
HlsSampleSource sampleSource = new HlsSampleSource(chunkSource, loadControl, MAIN_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE);
Hence its clear from the constructor that HlsSampleSource needs HlsChunkSource and for HlsChunkSource we
need DataSource. Refer to below figure:
HlsChunkSource is a temporary test source for HLS chunks. It contains chunks of video and audio before it is passed to HlsSampleSource.
Now we will see the code based implementation-
/* isMaster */,
In Line 1, LoadControl object is created and BUFFER_SEGMENT_SIZE is provided in the constructor. By default we keep this value as 64*1024.
In Line 2 & 3, DefaultBandwidthMeter & PtsTimestampAdjusterProvider objects are created which will be needed in the Renderer implementation.
In Line 4, DataSource object is created by passing context, bandwidthMeter object and userAgent. For obtaining userAgent we can use below code-
String userAgent = Util.getUserAgent(this, "ExoPlayerDemo");
In Line 5, HlsChunkSource object is created via DataSource object and in Line 6, HlsSampleSource object is created via HlsChunkSource object.
Now in Line 7 & 8, we can get the objects of MediaCodecVideoTrackRenderer & MediaCodecAudioTrackRenderer with the SampleSource object.
For more understanding you can refer to the sample project, which is the modified version of standard google exoplayer project:
In this sample you just need to enter the url which needs to be played and it starts playing seamlessly.
- See more at: http://www.tothenew.com/blog/how-to-play-hls-live-stream-using-exoplayer/#sthash.EA9WGhQd.dpuf