Ensure connecting state has passed before load/setClip return.

This commit is contained in:
Ryan Heise 2020-07-29 16:10:21 +10:00
parent ad51407079
commit 67d1b7450d
1 changed files with 18 additions and 9 deletions

View File

@ -308,7 +308,11 @@ class AudioPlayer {
Future<Duration> load(AudioSource source) async { Future<Duration> load(AudioSource source) async {
try { try {
_audioSource = source; _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) { } catch (e) {
_audioSource = null; _audioSource = null;
_audioSources.clear(); _audioSources.clear();
@ -345,14 +349,19 @@ class AudioPlayer {
/// original [AudioSource]. If [end] is null, it will be reset to the end of /// original [AudioSource]. If [end] is null, it will be reset to the end of
/// the original [AudioSource]. This method cannot be called from the /// the original [AudioSource]. This method cannot be called from the
/// [AudioPlaybackState.none] state. /// [AudioPlaybackState.none] state.
Future<Duration> setClip({Duration start, Duration end}) => Future<Duration> setClip({Duration start, Duration end}) async {
_load(start == null && end == null final duration = await _load(start == null && end == null
? _audioSource ? _audioSource
: ClippingAudioSource( : ClippingAudioSource(
audioSource: _audioSource, audioSource: _audioSource,
start: start, start: start,
end: end, 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] /// Plays the currently loaded media from the current position. The [Future]
/// returned by this method completes when playback completes or is paused or /// returned by this method completes when playback completes or is paused or