Displaying buffer state with ExoPlayer

This commit is contained in:
ByteHamster 2019-08-31 00:17:35 +02:00
parent 8a3e4f8765
commit c68f2f75ca
2 changed files with 29 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package de.danoeh.antennapod.core.service.playback;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.Handler;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
@ -27,7 +28,6 @@ import com.google.android.exoplayer2.util.Util;
import org.antennapod.audio.MediaPlayer; import org.antennapod.audio.MediaPlayer;
import de.danoeh.antennapod.core.util.playback.IPlayer; import de.danoeh.antennapod.core.util.playback.IPlayer;
public class ExoPlayerWrapper implements IPlayer { public class ExoPlayerWrapper implements IPlayer {
private final Context mContext; private final Context mContext;
private SimpleExoPlayer mExoPlayer; private SimpleExoPlayer mExoPlayer;
@ -35,15 +35,35 @@ public class ExoPlayerWrapper implements IPlayer {
private MediaPlayer.OnSeekCompleteListener audioSeekCompleteListener; private MediaPlayer.OnSeekCompleteListener audioSeekCompleteListener;
private MediaPlayer.OnCompletionListener audioCompletionListener; private MediaPlayer.OnCompletionListener audioCompletionListener;
private MediaPlayer.OnErrorListener audioErrorListener; private MediaPlayer.OnErrorListener audioErrorListener;
private MediaPlayer.OnBufferingUpdateListener bufferingUpdateListener;
private boolean shouldCheckBufferingUpdates = true;
ExoPlayerWrapper(Context context) { ExoPlayerWrapper(Context context) {
mContext = context; mContext = context;
mExoPlayer = createPlayer(); mExoPlayer = createPlayer();
Handler handler = new Handler(); // Main thread
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (bufferingUpdateListener != null) {
bufferingUpdateListener.onBufferingUpdate(null, mExoPlayer.getBufferedPercentage());
}
if (shouldCheckBufferingUpdates) {
handler.postDelayed(this, 2000);
}
}
}, 2000);
} }
private SimpleExoPlayer createPlayer() { private SimpleExoPlayer createPlayer() {
DefaultLoadControl.Builder loadControl = new DefaultLoadControl.Builder();
loadControl.setBufferDurationsMs(30000, 120000,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS,
DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS);
SimpleExoPlayer p = ExoPlayerFactory.newSimpleInstance(mContext, new DefaultRenderersFactory(mContext), SimpleExoPlayer p = ExoPlayerFactory.newSimpleInstance(mContext, new DefaultRenderersFactory(mContext),
new DefaultTrackSelector(), new DefaultLoadControl()); new DefaultTrackSelector(), loadControl.createDefaultLoadControl());
p.setSeekParameters(SeekParameters.PREVIOUS_SYNC); p.setSeekParameters(SeekParameters.PREVIOUS_SYNC);
p.addListener(new Player.EventListener() { p.addListener(new Player.EventListener() {
@Override @Override
@ -148,12 +168,14 @@ public class ExoPlayerWrapper implements IPlayer {
@Override @Override
public void release() { public void release() {
shouldCheckBufferingUpdates = false;
if (mExoPlayer != null) { if (mExoPlayer != null) {
mExoPlayer.release(); mExoPlayer.release();
} }
audioSeekCompleteListener = null; audioSeekCompleteListener = null;
audioCompletionListener = null; audioCompletionListener = null;
audioErrorListener = null; audioErrorListener = null;
bufferingUpdateListener = null;
} }
@Override @Override
@ -247,4 +269,8 @@ public class ExoPlayerWrapper implements IPlayer {
} }
return mExoPlayer.getVideoFormat().height; return mExoPlayer.getVideoFormat().height;
} }
void setOnBufferingUpdateListener(MediaPlayer.OnBufferingUpdateListener bufferingUpdateListener) {
this.bufferingUpdateListener = bufferingUpdateListener;
}
} }

View File

@ -1013,6 +1013,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
ExoPlayerWrapper ap = (ExoPlayerWrapper) mp; ExoPlayerWrapper ap = (ExoPlayerWrapper) mp;
ap.setOnCompletionListener(audioCompletionListener); ap.setOnCompletionListener(audioCompletionListener);
ap.setOnSeekCompleteListener(audioSeekCompleteListener); ap.setOnSeekCompleteListener(audioSeekCompleteListener);
ap.setOnBufferingUpdateListener(audioBufferingUpdateListener);
ap.setOnErrorListener(audioErrorListener); ap.setOnErrorListener(audioErrorListener);
} else { } else {
Log.w(TAG, "Unknown media player: " + mp); Log.w(TAG, "Unknown media player: " + mp);