-Modified recovery to not set if progress position is 0 or less.

-Modified queue item synchronization to no longer trigger update when the sync is run on the identical item.
This commit is contained in:
John Zhen Mo 2017-10-31 12:42:56 -07:00
parent 0b1eda3050
commit 01e031e7e7
2 changed files with 25 additions and 21 deletions

View File

@ -26,6 +26,7 @@ import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
@ -76,7 +77,6 @@ import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;
@ -193,7 +193,7 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen
.observeOn(AndroidSchedulers.mainThread())
.filter(new Predicate<Long>() {
@Override
public boolean test(@NonNull Long aLong) throws Exception {
public boolean test(Long aLong) throws Exception {
return isProgressLoopRunning();
}
})
@ -235,7 +235,7 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen
initPlayback(queue);
}
protected void initPlayback(@NonNull final PlayQueue queue) {
protected void initPlayback(final PlayQueue queue) {
playQueue = queue;
playQueue.init();
playbackManager = new MediaSourceManager(this, playQueue);
@ -514,11 +514,10 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen
}
break;
case Player.STATE_READY: //3
recover();
if (!isPrepared) {
isPrepared = true;
onPrepared(playWhenReady);
recover();
break;
}
if (currentState == STATE_PAUSED_SEEK) break;
@ -631,17 +630,21 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen
}
@Override
public void sync(@android.support.annotation.NonNull final PlayQueueItem item,
public void sync(@NonNull final PlayQueueItem item,
@Nullable final StreamInfo info) {
if (simpleExoPlayer == null) return;
if (DEBUG) Log.d(TAG, "Syncing...");
if (currentItem == item && currentInfo == info) return;
currentItem = item;
currentInfo = info;
if (DEBUG) Log.d(TAG, "Syncing...");
if (simpleExoPlayer == null) return;
// Check if on wrong window
final int currentSourceIndex = playQueue.getIndex();
if (simpleExoPlayer.getCurrentWindowIndex() != currentSourceIndex) {
final int currentSourceIndex = playQueue.indexOf(item);
if (currentSourceIndex != playQueue.getIndex()) {
throw new IllegalStateException("Play Queue may be desynchronized: item index=[" +
currentSourceIndex + "], queue index=[" + playQueue.getIndex() + "]");
} else if (simpleExoPlayer.getCurrentWindowIndex() != currentSourceIndex) {
final long startPos = info != null ? info.start_position : 0;
if (DEBUG) Log.d(TAG, "Rewinding to correct window: " + currentSourceIndex + " at: " + getTimeString((int)startPos));
simpleExoPlayer.seekTo(currentSourceIndex, startPos);
@ -900,7 +903,9 @@ public abstract class BasePlayer implements Player.EventListener, PlaybackListen
final int queuePos = playQueue.getIndex();
final long windowPos = simpleExoPlayer.getCurrentPosition();
setRecovery(queuePos, windowPos);
if (windowPos > 0) {
setRecovery(queuePos, windowPos);
}
}
public void setRecovery(final int queuePos, final long windowPos) {

View File

@ -52,7 +52,7 @@ public class MediaSourceManager {
public MediaSourceManager(@NonNull final PlaybackListener listener,
@NonNull final PlayQueue playQueue) {
this(listener, playQueue, 1, 1000L);
this(listener, playQueue, 1, 400L);
}
private MediaSourceManager(@NonNull final PlaybackListener listener,
@ -162,7 +162,7 @@ public class MediaSourceManager {
return;
}
// why no pattern matching in Java =(
// Event specific action
switch (event.type()) {
case INIT:
case REORDER:
@ -172,31 +172,28 @@ public class MediaSourceManager {
case APPEND:
populateSources();
break;
case SELECT:
sync();
break;
case REMOVE:
final RemoveEvent removeEvent = (RemoveEvent) event;
remove(removeEvent.getRemoveIndex());
// Sync only when the currently playing is removed
if (removeEvent.getQueueIndex() == removeEvent.getRemoveIndex()) sync();
break;
case MOVE:
final MoveEvent moveEvent = (MoveEvent) event;
move(moveEvent.getFromIndex(), moveEvent.getToIndex());
break;
case SELECT:
case RECOVERY:
default:
break;
}
// Loading and Syncing
switch (event.type()) {
case INIT:
case REORDER:
case ERROR:
case APPEND:
loadImmediate(); // low frequency, critical events
break;
case APPEND:
case REMOVE:
case SELECT:
case MOVE:
@ -294,7 +291,9 @@ public class MediaSourceManager {
final DeferredMediaSource mediaSource = (DeferredMediaSource) sources.getMediaSource(playQueue.indexOf(item));
if (mediaSource.state() == DeferredMediaSource.STATE_PREPARED) mediaSource.load();
if (tryUnblock()) sync();
tryUnblock();
if (!isBlocked) sync();
}
private void resetSources() {