Skip to content

Commit

Permalink
fix: player state not being updated to completed (#1257)
Browse files Browse the repository at this point in the history
This is regression introduced in 1.0.0.

In 0.2.0, this logic was:

      case 'audio.onComplete':
          player.state = PlayerState.COMPLETED;
          player._completionController.add(null);

So along with the completionController stream being update, the state was set to completed.
In 1.0.0 there is no such functionality:

      case 'audio.onComplete':
        emitComplete(playerId);

Since audioplayers_platform_interface package doesn't have a reference to player.state anymore because it's now in audioplayers package, my fix consisted of simply listening to this same stream in audioplayers package and updating the state.
  • Loading branch information
itsJoKr committed Sep 23, 2022
1 parent 5c0cb4e commit 70a37af
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/audioplayers/lib/src/audioplayer.dart
Expand Up @@ -36,6 +36,8 @@ class AudioPlayer {
_playerState = state;
}

late StreamSubscription _onPlayerCompleteStreamSubscription;

final StreamController<PlayerState> _playerStateController =
StreamController<PlayerState>.broadcast();

Expand Down Expand Up @@ -89,7 +91,11 @@ class AudioPlayer {
ReleaseMode get releaseMode => _releaseMode;

/// Creates a new instance and assigns an unique id to it.
AudioPlayer({String? playerId}) : playerId = playerId ?? _uuid.v4();
AudioPlayer({String? playerId}) : playerId = playerId ?? _uuid.v4() {
_onPlayerCompleteStreamSubscription = onPlayerComplete.listen((_) {
state = PlayerState.completed;
});
}

Future<void> play(
Source source, {
Expand Down Expand Up @@ -266,10 +272,11 @@ class AudioPlayer {
// First stop and release all native resources.
await release();

final futures = <Future>[];
if (!_playerStateController.isClosed) {
futures.add(_playerStateController.close());
}
final futures = <Future>[
if (!_playerStateController.isClosed) _playerStateController.close(),
_onPlayerCompleteStreamSubscription.cancel()
];

await Future.wait<dynamic>(futures);
}
}

0 comments on commit 70a37af

Please sign in to comment.