diff options
author | ByteHamster <info@bytehamster.com> | 2019-09-26 18:49:39 +0200 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2019-09-26 18:53:04 +0200 |
commit | 56d08c429119a288a6f8f8eddc41b80f2559ffb3 (patch) | |
tree | 426c8ef8830c7e049886e0275492d06667c7267a /core/src/main | |
parent | 39a9a48c9996661293ee0a34abc4b7945de74317 (diff) | |
download | AntennaPod-56d08c429119a288a6f8f8eddc41b80f2559ffb3.zip |
Using RxJava to update buffering
Diffstat (limited to 'core/src/main')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java index 7af33f8f9..af86db633 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/ExoPlayerWrapper.java @@ -2,7 +2,6 @@ package de.danoeh.antennapod.core.service.playback; import android.content.Context; import android.net.Uri; -import android.os.Handler; import android.view.SurfaceHolder; 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.util.Util; +import io.reactivex.Observable; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; import org.antennapod.audio.MediaPlayer; import de.danoeh.antennapod.core.util.playback.IPlayer; +import java.util.concurrent.TimeUnit; + public class ExoPlayerWrapper implements IPlayer { private final Context mContext; + private final Disposable bufferingUpdateDisposable; private SimpleExoPlayer mExoPlayer; private MediaSource mediaSource; private MediaPlayer.OnSeekCompleteListener audioSeekCompleteListener; private MediaPlayer.OnCompletionListener audioCompletionListener; private MediaPlayer.OnErrorListener audioErrorListener; private MediaPlayer.OnBufferingUpdateListener bufferingUpdateListener; - private boolean shouldCheckBufferingUpdates = true; ExoPlayerWrapper(Context context) { mContext = context; 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); + bufferingUpdateDisposable = Observable.interval(2, TimeUnit.SECONDS) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(aLong -> { + if (bufferingUpdateListener != null) { + bufferingUpdateListener.onBufferingUpdate(null, mExoPlayer.getBufferedPercentage()); + } + }); } private SimpleExoPlayer createPlayer() { @@ -168,7 +167,7 @@ public class ExoPlayerWrapper implements IPlayer { @Override public void release() { - shouldCheckBufferingUpdates = false; + bufferingUpdateDisposable.dispose(); if (mExoPlayer != null) { mExoPlayer.release(); } |