Improve seek behaviour

This commit is contained in:
Ryan Heise 2020-01-02 12:01:43 +11:00
parent 1fcfdefa20
commit 839890d707
1 changed files with 21 additions and 5 deletions

View File

@ -63,6 +63,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
private Long seekPos; private Long seekPos;
private Result prepareResult; private Result prepareResult;
private Result seekResult; private Result seekResult;
private boolean seekProcessed;
private MediaSource mediaSource; private MediaSource mediaSource;
private SimpleExoPlayer player; private SimpleExoPlayer player;
@ -101,6 +102,9 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
prepareResult = null; prepareResult = null;
transition(PlaybackState.stopped); transition(PlaybackState.stopped);
} }
if (seekProcessed) {
completeSeek();
}
break; break;
case Player.STATE_BUFFERING: case Player.STATE_BUFFERING:
// TODO: use this instead of checkForDiscontinuity. // TODO: use this instead of checkForDiscontinuity.
@ -116,14 +120,22 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
@Override @Override
public void onSeekProcessed() { public void onSeekProcessed() {
if (seekResult != null) { if (seekResult != null) {
seekPos = null; seekProcessed = true;
transition(stateBeforeSeek); if (player.getPlaybackState() == Player.STATE_READY) {
seekResult.success(null); completeSeek();
stateBeforeSeek = null; }
seekResult = null;
} }
} }
private void completeSeek() {
seekProcessed = false;
seekPos = null;
transition(stateBeforeSeek);
seekResult.success(null);
stateBeforeSeek = null;
seekResult = null;
}
private void checkForDiscontinuity() { private void checkForDiscontinuity() {
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
final long position = getCurrentPosition(); final long position = getCurrentPosition();
@ -332,8 +344,12 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
if (state == PlaybackState.none || state == PlaybackState.connecting) { if (state == PlaybackState.none || state == PlaybackState.connecting) {
throw new IllegalStateException("Cannot call seek from none none/connecting states"); throw new IllegalStateException("Cannot call seek from none none/connecting states");
} }
if (seekResult != null) {
seekResult.success(null);
}
seekPos = position; seekPos = position;
seekResult = result; seekResult = result;
seekProcessed = false;
if (stateBeforeSeek == null) { if (stateBeforeSeek == null) {
stateBeforeSeek = state; stateBeforeSeek = state;
} }