Since switching from Mediaplayer to a simple implementation Exoplayer I have noticed much improved load times but I\'m wondering if there is any built in functionality such
Posting to show the implementation that worked for me. Just a Singleton with start and stop methods and some intents to update the UI.
private void startStation(Station station){
if(station!=null) {
ExoPlayerSingleton.getInstance();
ExoPlayerSingleton.playStation(station, getApplicationContext());
}
}
public class ExoPlayerSingleton {
private static ExoPlayer mExoPlayerInstance;
private static MediaCodecAudioTrackRenderer audioRenderer;
private static final int BUFFER_SIZE = 10 * 1024 * 1024;
private static MediaPlayer mediaPlayer;
public static synchronized ExoPlayer getInstance() {
if (mExoPlayerInstance == null) {
mExoPlayerInstance = ExoPlayer.Factory.newInstance(1);
}
return mExoPlayerInstance;
}
public static synchronized ExoPlayer getCurrentInstance() {
return mExoPlayerInstance;
}
public static void stopExoForStation(Context context){
if(mExoPlayerInstance!=null) {
try {
mExoPlayerInstance.stop();
mExoPlayerInstance.release();
mExoPlayerInstance = null;
Intent intent = new Intent();
intent.setAction("com.zzz.now_playing_receiver");
context.sendBroadcast(intent);
} catch (Exception e) {
Log.e("Exoplayer Error", e.toString());
}
}
}
public static boolean isPlaying(){
if(mExoPlayerInstance!=null &&(mExoPlayerInstance.getPlaybackState()== ExoPlayer.STATE_READY )){
return true;
}else{
return false;
}
}
public static boolean isBuffering(){
if(mExoPlayerInstance!=null &&(mExoPlayerInstance.getPlaybackState()== ExoPlayer.STATE_BUFFERING)){
return true;
}else{
return false;
}
}
public static boolean isPreparing(){
if(mExoPlayerInstance!=null &&( mExoPlayerInstance.getPlaybackState()== ExoPlayer.STATE_PREPARING)){
return true;
}else{
return false;
}
}
public static void playStation(Station station,final Context context){
getInstance();
url = station.getLow_Stream();
if(url!=null) {
Uri uri = Uri.parse(url);
String userAgent = Util.getUserAgent(context, "SomeRadio");
DataSource audioDataSource = new DefaultUriDataSource(context,userAgent);
Mp3Extractor extractor = new Mp3Extractor();
ExtractorSampleSource sampleSource = new ExtractorSampleSource(
uri, audioDataSource,BUFFER_SIZE, extractor );
audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
mExoPlayerInstance.addListener(new ExoPlayer.Listener() {
@Override
public void onPlayerStateChanged(boolean b, int i) {
if (i == ExoPlayer.STATE_BUFFERING) {
} else if (i == ExoPlayer.STATE_IDLE) {
} else if (i == ExoPlayer.STATE_ENDED) {
} else if (i == ExoPlayer.STATE_READY) {
Intent intent = new Intent();
intent.setAction("com.zzz.pause_play_update");
context.sendBroadcast(intent);
Intent progress_intent = new Intent();
progress_intent.putExtra("show_dialog", false);
progress_intent.setAction("com.zzz.load_progess");
context.sendBroadcast(progress_intent);
}
}
@Override
public void onPlayWhenReadyCommitted() {
}
@Override
public void onPlayerError(ExoPlaybackException e) {
String excep = e.toString();
Log.e("ExoPlayer Error",excep);
}
});
mExoPlayerInstance.prepare(audioRenderer);
mExoPlayerInstance.setPlayWhenReady(true);
}else{
//send intent to raise no connection dialog
}
}