From fda0b872e649e3f4e4015d8853fa1fccfa72b4c6 Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Tue, 22 Dec 2020 23:53:12 +1100 Subject: [PATCH] Fix bug in play result on simultaneous requests. --- just_audio/lib/just_audio.dart | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/just_audio/lib/just_audio.dart b/just_audio/lib/just_audio.dart index 7bd8f3b..ba71ac5 100644 --- a/just_audio/lib/just_audio.dart +++ b/just_audio/lib/just_audio.dart @@ -86,7 +86,6 @@ class AudioPlayer { bool _automaticallyWaitsToMinimizeStalling = true; bool _playInterrupted = false; AndroidAudioAttributes _androidAudioAttributes; - Completer _playCompleter; /// Creates an [AudioPlayer]. The player will automatically pause/duck and /// resume/unduck when audio interruptions occur (e.g. a phone call) or when @@ -684,7 +683,7 @@ class AudioPlayer { // activate the audio session. This allows setAudioSource to be aware of a // prior play request. _playingSubject.add(true); - _playCompleter = Completer(); + final playCompleter = Completer(); final audioSession = await AudioSession.instance; if (await audioSession.setActive(true)) { // TODO: rewrite this to more cleanly handle simultaneous load/play @@ -696,19 +695,18 @@ class AudioPlayer { // NOTE: If a load() request happens simultaneously, this may result // in two play requests being sent. The platform implementation should // ignore the second play request since it is already playing. - _sendPlayRequest(await _platform); + _sendPlayRequest(await _platform, playCompleter); } else { // If the native platform wasn't already active, activating it will // implicitly restore the playing state and send a play request. - _setPlatformActive(true); + _setPlatformActive(true, playCompleter); } } } else { // Revert if we fail to activate the audio session. _playingSubject.add(false); } - await _playCompleter?.future; - _playCompleter = null; + await playCompleter.future; } /// Pauses the currently playing media. This method does nothing if @@ -730,11 +728,10 @@ class AudioPlayer { await (await _platform).pause(PauseRequest()); } - Future _sendPlayRequest(AudioPlayerPlatform platform) async { + Future _sendPlayRequest( + AudioPlayerPlatform platform, Completer playCompleter) async { await platform.play(PlayRequest()); - if (_playCompleter?.isCompleted == false) { - _playCompleter.complete(); - } + playCompleter?.complete(); } /// Stops playing audio and releases decoders and other native platform @@ -906,7 +903,8 @@ class AudioPlayer { /// idle platform when [active] is `false`. If an audio source has been set, /// the returned future completes with its duration if known, or `null` /// otherwise. - Future _setPlatformActive(bool active) { + Future _setPlatformActive(bool active, + [Completer playCompleter]) { if (active == _active) return _durationFuture; // This method updates _active and _platform before yielding to the next // task in the event loop. @@ -991,7 +989,7 @@ class AudioPlayer { ? ShuffleModeMessage.all : ShuffleModeMessage.none)); if (playing) { - _sendPlayRequest(platform); + _sendPlayRequest(platform, playCompleter); } } if (audioSource != null) {