Using RxJava to update buffering

This commit is contained in:
ByteHamster 2019-09-26 18:49:39 +02:00
parent 39a9a48c99
commit 56d08c4291
1 changed files with 14 additions and 15 deletions

View File

@ -2,7 +2,6 @@ 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;
@ -25,36 +24,36 @@ import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
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;
import java.util.concurrent.TimeUnit;
public class ExoPlayerWrapper implements IPlayer { public class ExoPlayerWrapper implements IPlayer {
private final Context mContext; private final Context mContext;
private final Disposable bufferingUpdateDisposable;
private SimpleExoPlayer mExoPlayer; private SimpleExoPlayer mExoPlayer;
private MediaSource mediaSource; private MediaSource mediaSource;
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 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 bufferingUpdateDisposable = Observable.interval(2, TimeUnit.SECONDS)
handler.postDelayed(new Runnable() { .observeOn(AndroidSchedulers.mainThread())
@Override .subscribe(aLong -> {
public void run() { if (bufferingUpdateListener != null) {
if (bufferingUpdateListener != null) { bufferingUpdateListener.onBufferingUpdate(null, mExoPlayer.getBufferedPercentage());
bufferingUpdateListener.onBufferingUpdate(null, mExoPlayer.getBufferedPercentage()); }
} });
if (shouldCheckBufferingUpdates) {
handler.postDelayed(this, 2000);
}
}
}, 2000);
} }
private SimpleExoPlayer createPlayer() { private SimpleExoPlayer createPlayer() {
@ -168,7 +167,7 @@ public class ExoPlayerWrapper implements IPlayer {
@Override @Override
public void release() { public void release() {
shouldCheckBufferingUpdates = false; bufferingUpdateDisposable.dispose();
if (mExoPlayer != null) { if (mExoPlayer != null) {
mExoPlayer.release(); mExoPlayer.release();
} }