1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-22 11:02:35 +01:00

fix race condition when returning to main player

This commit is contained in:
Christian Schabesberger 2018-10-26 14:59:49 +02:00
parent afaba7ccdf
commit e1cc006db7

View File

@ -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();
}
}