Improve state management

This commit is contained in:
Ryan Heise 2020-01-01 19:39:49 +11:00
parent b10d13d413
commit d2b3fca67f
2 changed files with 16 additions and 10 deletions

View File

@ -293,9 +293,10 @@ public class AudioPlayer implements MethodCallHandler, Player.EventListener {
case stopped: case stopped:
result.success(null); result.success(null);
break; break;
// TODO: Allow stopping from buffered/connecting states. // TODO: Allow stopping from connecting states.
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);

View File

@ -194,15 +194,20 @@ class AudioPlayer {
Future<void> play() async { Future<void> play() async {
StreamSubscription subscription; StreamSubscription subscription;
Completer completer = Completer(); Completer completer = Completer();
subscription = playbackStateStream bool startedPlaying = false;
.skip(1) subscription = playbackStateStream.listen((state) {
.where((state) => // TODO: It will be more reliable to let the platform
state == AudioPlaybackState.paused || // side wait for completion since events on the flutter
// side can lag behind the platform side.
if (startedPlaying &&
(state == AudioPlaybackState.paused ||
state == AudioPlaybackState.stopped || state == AudioPlaybackState.stopped ||
state == AudioPlaybackState.completed) state == AudioPlaybackState.completed)) {
.listen((state) {
subscription.cancel(); subscription.cancel();
completer.complete(); completer.complete();
} else if (state == AudioPlaybackState.playing) {
startedPlaying = true;
}
}); });
await _invokeMethod('play'); await _invokeMethod('play');
await completer.future; await completer.future;