Improve state management
This commit is contained in:
parent
b10d13d413
commit
d2b3fca67f
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue