From e1cc006db7929b99bf5566f06c7dd0ef6e54ab63 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Fri, 26 Oct 2018 14:59:49 +0200 Subject: [PATCH] fix race condition when returning to main player --- .../org/schabi/newpipe/player/BasePlayer.java | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index f5f843caa..e44b4d3cb 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -145,6 +145,10 @@ public abstract class BasePlayer implements @Nullable protected Toast errorToast; + /** Will remember the last playback state even if simpleExoPlayer ot destroyed */ + private int lastPlaybackState; + private boolean lastPlayWhenReady; + /*////////////////////////////////////////////////////////////////////////// // Player //////////////////////////////////////////////////////////////////////////*/ @@ -273,6 +277,8 @@ public abstract class BasePlayer implements public void destroyPlayer() { if (DEBUG) Log.d(TAG, "destroyPlayer() called"); if (simpleExoPlayer != null) { + lastPlaybackState = simpleExoPlayer.getPlaybackState(); + lastPlayWhenReady = simpleExoPlayer.getPlayWhenReady(); simpleExoPlayer.removeListener(this); simpleExoPlayer.stop(); simpleExoPlayer.release(); @@ -1106,14 +1112,16 @@ public abstract class BasePlayer implements } public boolean isPlaying() { - final int state = simpleExoPlayer.getPlaybackState(); + final int state = getLastPlaybackState(); return (state == Player.STATE_READY || state == Player.STATE_BUFFERING) - && simpleExoPlayer.getPlayWhenReady(); + && getLastPlayWhenReady(); } @Player.RepeatMode public int getRepeatMode() { - return simpleExoPlayer == null ? Player.REPEAT_MODE_OFF : simpleExoPlayer.getRepeatMode(); + return simpleExoPlayer == null + ? Player.REPEAT_MODE_OFF + : simpleExoPlayer.getRepeatMode(); } public void setRepeatMode(@Player.RepeatMode final int repeatMode) { @@ -1179,4 +1187,22 @@ public abstract class BasePlayer implements if (DEBUG) Log.d(TAG, "Setting recovery, queue: " + queuePos + ", pos: " + windowPos); playQueue.setRecovery(queuePos, windowPos); } + + /** + * Sometimes the playbac kstate gets asked even though simpleExoPlayer got already destroyed. + * Therefore on destroying simpleExoPlayer the playback state will get asked once more, so + * you can retreve the last playback state here. + * @return the playback state of simpleExoPlayer even if got destroyed + */ + public int getLastPlaybackState() { + return simpleExoPlayer == null + ? lastPlaybackState + : simpleExoPlayer.getPlaybackState(); + } + + public boolean getLastPlayWhenReady() { + return simpleExoPlayer == null + ? lastPlayWhenReady + : simpleExoPlayer.getPlayWhenReady(); + } }