From 67d1b7450dc4dcbbd09a5a164d529b805174b59a Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Wed, 29 Jul 2020 16:10:21 +1000 Subject: [PATCH] Ensure connecting state has passed before load/setClip return. --- lib/just_audio.dart | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/just_audio.dart b/lib/just_audio.dart index cfe9cdd..70a8f30 100644 --- a/lib/just_audio.dart +++ b/lib/just_audio.dart @@ -308,7 +308,11 @@ class AudioPlayer { Future load(AudioSource source) async { try { _audioSource = source; - return await _load(source); + final duration = await _load(source); + // Wait for connecting state to pass. + await playbackStateStream + .firstWhere((state) => state != AudioPlaybackState.connecting); + return duration; } catch (e) { _audioSource = null; _audioSources.clear(); @@ -345,14 +349,19 @@ class AudioPlayer { /// original [AudioSource]. If [end] is null, it will be reset to the end of /// the original [AudioSource]. This method cannot be called from the /// [AudioPlaybackState.none] state. - Future setClip({Duration start, Duration end}) => - _load(start == null && end == null - ? _audioSource - : ClippingAudioSource( - audioSource: _audioSource, - start: start, - end: end, - )); + Future setClip({Duration start, Duration end}) async { + final duration = await _load(start == null && end == null + ? _audioSource + : ClippingAudioSource( + audioSource: _audioSource, + start: start, + end: end, + )); + // Wait for connecting state to pass. + await playbackStateStream + .firstWhere((state) => state != AudioPlaybackState.connecting); + return duration; + } /// Plays the currently loaded media from the current position. The [Future] /// returned by this method completes when playback completes or is paused or