Exoplayer playing m3u8 files Android

I Have created one demo app to play m3u8 media file

First add gradle dependencies to your file

compile 'com.google.android.exoplayer:exoplayer:r2.4.0'

Create a simple layout file with storage master.m3u8 path as input and SimpleExoPlayerView view to play downloaded files.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout mlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent" android:layout_height="match_parent">

<android.support.design.widget.TextInputEditText
  android:layout_marginTop="15dp"
android:layout_width="match_parent"
   android:id="@+id/mediaPath"
   android:layout_height="56dp" />
   <android.support.v7.widget.AppCompatButton
android:id="@+id/play"
       android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
       android:layout_below="@+id/mediaPath"
       android:background="@color/colorAccent"
android:layout_width="match_parent"
android:text="Play"
android:layout_height="56dp" />
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
   android:layout_below="@+id/play"
   android:id="@+id/video_view"
   android:layout_marginTop="5dp"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>
</RelativeLayout>

Create a simple ExoPlayer Activity to play Files.

public class ExoPlayer  extends AppCompatActivity{
Context mContext;
SimpleExoPlayerView playerView;
EditText editText;
private ComponentListener componentListener;
private DataSource.Factory mediaDataSourceFactory;
private Handler mainHandler;
private DefaultTrackSelector trackSelector;
SimpleExoPlayer player;
private static final DefaultBandwidthMeter BANDWIDTH_METER = new        DefaultBandwidthMeter();




@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_exoplayer);
   mContext =this;
   editText = (EditText) findViewById(R.id.mediaPath);
   Button btnPlay = (Button) findViewById(R.id.play);
   playerView  = (SimpleExoPlayerView)findViewById(R.id.video_view);
   mediaDataSourceFactory = buildDataSourceFactory(true);
   mainHandler = new Handler();
   componentListener = new ComponentListener();
   trackSelector = new DefaultTrackSelector();
   btnPlay.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
      if(editText.getText()!=null && editText.getText().length()>0){
               initializePlayer(editText.getText().toString());
           }
       }
   });
}
   private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
   return ((AnalyticsApplication) getApplication())
           .buildDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null);
 }

   private void initializePlayer(String path) {
   player = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
   player.addListener(componentListener);
//        String path = file:///storage/emulated/0/SugarBox/master.m3u8";
   Uri uri = Uri.parse(path);
   MediaSource mediaSource = buildMediaSource(uri);
   player.prepare(mediaSource, true, false);
   playerView.setPlayer(player);
 }
  private MediaSource buildMediaSource(Uri uri) {
 return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, null);
 }
 private void releasePlayer() {
   if (player != null) {
       player.removeListener(componentListener);
       player.release();
       player = null;
   }
   }



 @Override
 public void onStart() {
   super.onStart();
   if (Util.SDK_INT > 23) {
       if(editText.getText()!=null && editText.getText().length()>0){
           initializePlayer(editText.getText().toString());
       }
   }
 }




@Override
   public void onResume() {
   super.onResume();
   if ((Util.SDK_INT <= 23 || player == null)) {
       if(editText.getText()!=null && editText.getText().length()>0){
           initializePlayer(editText.getText().toString());
       }
   }
 }



@Override
 public void onPause() {
   super.onPause();
   if (Util.SDK_INT <= 23) {
       releasePlayer();
   }
}



 @Override
public void onStop() {
   super.onStop();
   if (Util.SDK_INT > 23) {
       releasePlayer();
   }
}







   private class ComponentListener implements com.google.android.exoplayer2.ExoPlayer.EventListener{

   @Override
   public void onPlayerStateChanged(boolean playWhenReady,
                                    int playbackState) {
       String stateString;
       switch (playbackState) {
           case com.google.android.exoplayer2.ExoPlayer.STATE_IDLE:
               stateString = "ExoPlayer.STATE_IDLE      -";
               break;
           case com.google.android.exoplayer2.ExoPlayer.STATE_BUFFERING:
               stateString = "ExoPlayer.STATE_BUFFERING -";
               break;
           case com.google.android.exoplayer2.ExoPlayer.STATE_READY:
               stateString = "ExoPlayer.STATE_READY     -";
               break;
           case com.google.android.exoplayer2.ExoPlayer.STATE_ENDED:
               stateString = "ExoPlayer.STATE_ENDED     -";
               break;
           default:
               stateString = "UNKNOWN_STATE             -";
               break;
       }
       Log.d("ExopLayer", "changed state to " + stateString
               + " playWhenReady: " + playWhenReady);
   }

   @Override
   public void onTimelineChanged(Timeline timeline, Object manifest) {}
   @Override
   public void onTracksChanged(TrackGroupArray trackGroups,
                               TrackSelectionArray trackSelections) {}
   @Override
   public void onLoadingChanged(boolean isLoading) {}
   @Override
   public void onPlayerError(ExoPlaybackException error) {}
   @Override
   public void onPositionDiscontinuity() {}
   @Override
   public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {}
  }



private DrmSessionManager<FrameworkMediaCrypto> buildDrmSessionManager(UUID uuid,   String licenseUrl, String[] keyRequestPropertiesArray) throws UnsupportedDrmException {
   if (Util.SDK_INT < 18) {
       return null;
   }
   HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(licenseUrl,
           buildHttpDataSourceFactory(false));
   if (keyRequestPropertiesArray != null) {
       for (int i = 0; i < keyRequestPropertiesArray.length - 1; i += 2) {
           drmCallback.setKeyRequestProperty(keyRequestPropertiesArray[i],
                   keyRequestPropertiesArray[i + 1]);
       }
   }
   return new DefaultDrmSessionManager<>(uuid,
           FrameworkMediaDrm.newInstance(uuid), drmCallback, null, mainHandler, null);
 }




  private HttpDataSource.Factory buildHttpDataSourceFactory(boolean useBandwidthMeter) {
   return ((AnalyticsApplication) getApplication())
           .buildHttpDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null);


  }


}

On Android 4.1+, you can use this library https://github.com/brianwernick/ExoMedia/ . The example mentioned on the Read-me page should be sufficient to get you started. I have reproduced that code snippet with a few additions/modifications.

            private void setupVideoView() {
                EMVideoView emVideoView = (EMVideoView)findViewById(R.id.video_play_activity_video_view);
                emVideoView.setOnPreparedListener(this);

                //Enter your m3u8 URL below
                emVideoView.setVideoURI(Uri.parse("http://SOMESERVER/playlist.m3u8"));
            }

            @Override
            public void onPrepared(MediaPlayer mp) {
                //Starts the video playback as soon as it is ready
                emVideoView.start();
            }

            @Override
            public void onPause() {
                super.onPause();
                //Pause Video Playback
                emVideoView.pause();
            }