package com.benbvs.cordova.exoplayer;

import com.google.android.exoplayer2.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import android.R;
import android.content.Context;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.os.Handler;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import android.net.Uri;
 
public class exoplayer extends CordovaPlugin {
 
  public Boolean playerInitialised = false;
  private SimpleExoPlayer exoPlayer;
  
  private static final int BUFFER_SEGMENT_SIZE = 64 * 1024;
  private static final int BUFFER_SEGMENT_COUNT = 256;
  
  
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if("initPlayer".equals(action)){
	  
      	callbackContext.success();
      	return true;
    }else if("play".equals(action)){
		if(args.length() == 1 && !exoPlayer.getPlayWhenReady()){
			if(!playerInitialised) { 
				Handler handler = new Handler();
				TrackSelector trackSelector = new DefaultTrackSelector(handler);
				LoadControl loadControl = new DefaultLoadControl();
				exoPlayer = ExoPlayerFactory.newSimpleInstance(this.cordova.getActivity().getApplicationContext(), trackSelector, loadControl);
				playerInitialised=true;
			  }
			Uri audioUri = Uri.parse(args.getString(0));
			DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("ExoPlayerDemo");
			ExtractorsFactory extractor = new DefaultExtractorsFactory();
			MediaSource audioSource = new ExtractorMediaSource(audioUri, dataSourceFactory, extractor, null, null);
			exoPlayer.prepare(audioSource);
			exoPlayer.setPlayWhenReady(true);
			callbackContext.success();
		}
      return true;
    }else if("stop".equals(action)){
		if(playerInitialised && exoPlayer.getPlayWhenReady()){			
			exoPlayer.stop();
			exoPlayer.release();
			callbackContext.success();
		}
      return true;
    } else {
      callbackContext.error("AlertPlugin."+action+" not found !");
      return false;
    }
  }
  
   
}

@@ -1310,167 +1310,8 @@
 %0A%09  
-%0D%0A      %09callbackContext.success();%0D%0A      %09return true;%0D%0A    %7Delse if(%22play%22.equals(action))%7B%0D%0A%09%09if(args.length() == 1 && !exoPlayer.getPlayWhenReady())%7B%0D%0A%09%09%09
 if(!
@@ -1333,18 +1333,16 @@
 ed) %7B %0D%0A
-%09%09
 %09%09Handle
@@ -1369,18 +1369,16 @@
 ler();%0D%0A
-%09%09
 %09%09TrackS
@@ -1437,18 +1437,16 @@
 dler);%0D%0A
-%09%09
 %09%09LoadCo
@@ -1494,18 +1494,16 @@
 l();%0D%0A%09%09
-%09
-%09
 exoPlaye
@@ -1623,18 +1623,16 @@
 trol);%0D%0A
-%09%09
 %09%09player
@@ -1655,13 +1655,186 @@
 ;%0D%0A%09
-%09%09
   %7D
+%0D%0A      %09callbackContext.success();%0D%0A      %09return true;%0D%0A    %7Delse if(%22play%22.equals(action))%7B%0D%0A%09%09if(args.length() == 1 && playerInitialised && !exoPlayer.getPlayWhenReady())%7B
 %0D%0A%09%09
@@ -2408,24 +2408,26 @@
 stop();%0D%0A%09%09%09
+//
 exoPlayer.re

Update exoplayer.java