Skip to content

Commit

Permalink
fix: Cancel onPreparedSubscription on error (#1660)
Browse files Browse the repository at this point in the history
# Description

If an error occurred during setting the source, the
`onPreparedSubscription` was still listened to, although it is not
needed anymore. This triggered a `Future already completed` error.

## Related Issues

Fixes #1642
  • Loading branch information
Gustl22 committed Nov 14, 2023
1 parent 409f5d5 commit d008958
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/audioplayers/lib/src/audioplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,21 +302,23 @@ class AudioPlayer {

Future<void> _completePrepared(Future<void> Function() fun) async {
final preparedCompleter = Completer<void>();
final onPreparedSubscription = _onPrepared.listen(
(isPrepared) {
late StreamSubscription<bool> onPreparedSubscription;
onPreparedSubscription = _onPrepared.listen(
(isPrepared) async {
if (isPrepared) {
preparedCompleter.complete();
await onPreparedSubscription.cancel();
}
},
onError: (Object e, [StackTrace? stackTrace]) {
onError: (Object e, [StackTrace? stackTrace]) async {
if (!preparedCompleter.isCompleted) {
preparedCompleter.completeError(e, stackTrace);
await onPreparedSubscription.cancel();
}
},
);
await fun();
await preparedCompleter.future.timeout(const Duration(seconds: 30));
onPreparedSubscription.cancel();
}

/// Sets the URL to a remote link.
Expand Down

0 comments on commit d008958

Please sign in to comment.