How to Play HLS Videos on Android

Android
Social sharing

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.


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

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

XML

<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>

Java

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);

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.

@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);
}

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 [email protected], we would love to help!

Your recently viewed posts:

    Contact Us

    We’d love to help & work with you




    When do you want to start ?


    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box