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:
result.success(null);
break;
// TODO: Allow stopping from buffered/connecting states.
// TODO: Allow stopping from connecting states.
case completed:
case playing:
case buffering:
case paused:
player.setPlayWhenReady(false);
player.seekTo(0L);

View File

@ -194,15 +194,20 @@ class AudioPlayer {
Future<void> play() async {
StreamSubscription subscription;
Completer completer = Completer();
subscription = playbackStateStream
.skip(1)
.where((state) =>
state == AudioPlaybackState.paused ||
state == AudioPlaybackState.stopped ||
state == AudioPlaybackState.completed)
.listen((state) {
subscription.cancel();
completer.complete();
bool startedPlaying = false;
subscription = playbackStateStream.listen((state) {
// TODO: It will be more reliable to let the platform
// 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.completed)) {
subscription.cancel();
completer.complete();
} else if (state == AudioPlaybackState.playing) {
startedPlaying = true;
}
});
await _invokeMethod('play');
await completer.future;