How to Play HLS Videos on Android

Android

How to Play HLS Videos on Android

Playing videos/audios is a popular activity on Android devices. Its framework provides MediaPlayer API as a quick integration to play media files with minimal effort.

What is HLS

HTTP Live Streaming (HLS) is an HTTP-based media streaming communications protocol implemented by Apple Inc. It’s just like MPEG-DASH which works by splitting the whole stream into a sequence of small HTTP-based file downloads, with each download loading one short block of an overall potentially unbounded transport stream.

How HLS Works

HLS works just like all robust streaming technologies. You can create multiple files for distribution to the player, which can adaptively change streams to enhance the playback experience. No streaming server is required for it, as it is an HTTP-based technology. Hence, all the switching logic resides on the player.

To distribute a video to the clients, the source needs to be encoded into multiple files at different data rates and then divided into short chunks (usually between 5-10 seconds long). Then, the files are loaded onto an HTTP server along with a text-based manifest file with an “.M3U8” extension.

Introduction to ExoPlayer

ExoPlayer is an app level media player API for Android, providing an alternative to Android’s default Media Player API from both Local Devices and the internet streams. Compared to Android’s Media Player, it’s easier to personalize; Supports features not compatible with Android Media Player API, such as HLS, DASH & Smooth Streaming Adaptive Playbacks.

To integrate the ExoPlayer into the Android app, you need to add the following dependency in your project’s build.gradle file.


[code language=”php”]
compile ‘com.google.android.exoplayer:exoplayer:rX.X.X’
[/code]

Here’s the code for Streaming the HLS video in Exoplayer

XML

[code language=”html”]
<com.google.android.exoplayer.AspectRatioFrameLayout android:id="@+id/exoplayer_frame" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center">

<SurfaceView android:id="@+id/surface_view_exoplayer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center"/>

<View android:id="@+id/view_temp" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black"/>

<com.google.android.exoplayer.text.SubtitleLayout android:id="@+id/subtitles_exoplayer" android:layout_width="match_parent" android:layout_height="match_parent"/>

</com.google.android.exoplayer.AspectRatioFrameLayout>
[/code]

Java

[code language=”php”]
if (player == null) {

exoplayer_player = new DemoPlayer(getRendererBuilder(THE HLS VIDEO URL));

exoplayer_player .addListener(this);

exoplayer_player .setCaptionListener(this);

exoplayer_player .setMetadataListener(this);

exoplayer_player .seekTo(playerPosition);

playerNeedsPrepare = true;

mediaController.setMediaPlayer(exoplayer_player .getPlayerControl());

mediaController.setEnabled(true);

exoplayer_eventLogger = new EventLogger();

exoplayer_eventLogger .startSession();

exoplayer_player .addListener(eventLogger);

exoplayer_player .setInfoListener(eventLogger);

exoplayer_player .setInternalErrorListener(eventLogger);

exoplayer_debugViewHelper = new DebugTextViewHelper(player, debugTextView);

exoplayer_debugViewHelper .start();

}

exoplayer_player .setSurface(surfaceView.getHolder().getSurface());

exoplayer_player .setPlayWhenReady(playWhenReady);
[/code]

This is a Override method which handles the video resizing while streaming video in the player. It also handles the video size while the user changing the orientation of the device.

[code language=”html”]
@Override
public void onVideoSizeChanged(int video_width, int video_height, int video_unappliedRotationDegrees,float video_pixelWidthHeightRatio)

{
surfaceView_exoplayer.setVideoWidthHeightRatio(
video_height == 0 ? 1 : (video_width * video_pixelWidthAspectRatio) / video_height);
}
[/code]

Conclusion

●     ExoPlayer supports Dynamic Adaptive Streaming over HTTP (DASH) and Smooth Streaming which              aren’t supported by the default Android Media Player.
●     It supports advanced HLS features, such as correct handling of #EXT-X-DISCONTINUITY tags.
●     It’s highly customizable, which will suit your case.
●     It easily updates the player along with your application.

References: Github, Android Developers 

Are you having problems in HLS in Android devices? Share your pain points with me at info@andolasoft.com, we would love to help!

Tags:
Jay
Jayadev Das
jayadev.das@andolasoft.com

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘bout Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!

Shares