Improve state handling

This commit is contained in:
Ryan Heise 2020-02-05 01:27:40 +11:00
parent 67bcd13739
commit 280f1a8208
1 changed files with 25 additions and 7 deletions

View File

@ -285,10 +285,14 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
case buffering: case buffering:
case paused: case paused:
player.setPlayWhenReady(true); player.setPlayWhenReady(true);
if (seekResult != null) {
stateBeforeSeek = PlaybackState.playing;
} else {
transition(PlaybackState.playing); transition(PlaybackState.playing);
}
break; break;
default: default:
throw new IllegalStateException("Cannot call play from connecting or none states (" + state + ")"); throw new IllegalStateException("Cannot call play from connecting/none states (" + state + ")");
} }
} }
@ -300,6 +304,9 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
case buffering: case buffering:
player.setPlayWhenReady(false); player.setPlayWhenReady(false);
transition(PlaybackState.paused); transition(PlaybackState.paused);
if (seekResult != null) {
stateBeforeSeek = PlaybackState.paused;
}
break; break;
default: default:
throw new IllegalStateException("Can call pause only from playing and buffering states (" + state + ")"); throw new IllegalStateException("Can call pause only from playing and buffering states (" + state + ")");
@ -312,12 +319,15 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
result.success(null); result.success(null);
break; break;
case connecting: case connecting:
abortExistingConnection();
transition(PlaybackState.stopped); transition(PlaybackState.stopped);
result.success(null); result.success(null);
break; break;
case buffering:
abortSeek();
// no break
case completed: case completed:
case playing: case playing:
case buffering:
case paused: case paused:
player.setPlayWhenReady(false); player.setPlayWhenReady(false);
player.seekTo(0L); player.seekTo(0L);
@ -325,7 +335,7 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
result.success(null); result.success(null);
break; break;
default: default:
throw new IllegalStateException("Can call stop only from playing/paused/completed states (" + state + ")"); throw new IllegalStateException("Cannot call stop from none state");
} }
} }
@ -344,9 +354,7 @@ 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) { abortSeek();
seekResult.success(null);
}
seekPos = position; seekPos = position;
seekResult = result; seekResult = result;
seekProcessed = false; seekProcessed = false;
@ -363,6 +371,16 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
transition(PlaybackState.none); transition(PlaybackState.none);
} }
private void abortSeek() {
if (seekResult != null) {
seekResult.success(null);
seekResult = null;
seekPos = null;
stateBeforeSeek = null;
seekProcessed = false;
}
}
private void abortExistingConnection() { private void abortExistingConnection() {
if (prepareResult != null) { if (prepareResult != null) {
prepareResult.success(null); prepareResult.success(null);