Skip to content

Commit

Permalink
fix: Wait for seek to complete (#1712)
Browse files Browse the repository at this point in the history
# Description

Wait for seek to finish by listening to the
`AudioEventType.seekComplete` event.
  • Loading branch information
Gustl22 committed Nov 30, 2023
1 parent 577a3ff commit fd33b1d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
4 changes: 0 additions & 4 deletions packages/audioplayers/example/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ class _PlayerWidgetState extends State<PlayerWidget> {
}
Future<void> _play() async {
final position = _position;
if (position != null && position.inMilliseconds > 0) {
await player.seek(position);
}
await player.resume();
setState(() => _playerState = PlayerState.playing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ class _PlayerWidgetState extends State<PlayerWidget> {
}

Future<void> _play() async {
final position = _position;
if (position != null && position.inMilliseconds > 0) {
await player.seek(position);
}
await player.resume();
setState(() => _playerState = PlayerState.playing);
}
Expand Down
25 changes: 18 additions & 7 deletions packages/audioplayers/lib/src/audioplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ class AudioPlayer {

late final StreamSubscription _onPlayerCompleteStreamSubscription;

late final StreamSubscription _onSeekCompleteStreamSubscription;

late final StreamSubscription _onLogStreamSubscription;

/// Stream controller to be able to get a stream on initialization, before the
Expand Down Expand Up @@ -164,9 +162,6 @@ class AudioPlayer {
/* Errors are already handled via log stream */
},
);
_onSeekCompleteStreamSubscription = onSeekComplete.listen((event) async {
await _positionUpdater?.update();
});
_create();
positionUpdater = FramePositionUpdater(
getPosition: getCurrentPosition,
Expand Down Expand Up @@ -290,7 +285,24 @@ class AudioPlayer {
/// Moves the cursor to the desired position.
Future<void> seek(Duration position) async {
await creatingCompleter.future;
return _platform.seek(playerId, position);

final seekCompleter = Completer<void>();
late StreamSubscription<void> onSeekCompleteSubscription;
onSeekCompleteSubscription = onSeekComplete.listen(
(_) async {
seekCompleter.complete();
await onSeekCompleteSubscription.cancel();
},
onError: (Object e, [StackTrace? stackTrace]) async {
if (!seekCompleter.isCompleted) {
seekCompleter.completeError(e, stackTrace);
await onSeekCompleteSubscription.cancel();
}
},
);
await _platform.seek(playerId, position);
await seekCompleter.future.timeout(const Duration(seconds: 30));
await _positionUpdater?.update();
}

/// Sets the stereo balance.
Expand Down Expand Up @@ -461,7 +473,6 @@ class AudioPlayer {
if (_positionUpdater != null) _positionUpdater!.dispose(),
if (!_playerStateController.isClosed) _playerStateController.close(),
_onPlayerCompleteStreamSubscription.cancel(),
_onSeekCompleteStreamSubscription.cancel(),
_onLogStreamSubscription.cancel(),
_eventStreamSubscription.cancel(),
_eventStreamController.close(),
Expand Down
18 changes: 16 additions & 2 deletions packages/audioplayers_linux/linux/audio_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ void AudioPlayer::OnPlaybackEnded() {
if (GetLooping()) {
Play();
} else {
Pause();
SetPosition(0);
Stop();
}
}

Expand Down Expand Up @@ -412,6 +411,21 @@ void AudioPlayer::Pause() {
}
}

void AudioPlayer::Stop() {
Pause();
if (!_isInitialized) {
return;
}
SetPosition(0);
// Block thread to wait for state, as it is not expected to be waited to
// "seek complete" event on the dart side.
GstStateChangeReturn ret =
gst_element_get_state(playbin, NULL, NULL, GST_CLOCK_TIME_NONE);
if (ret == GST_STATE_CHANGE_FAILURE) {
throw "Unable to seek playback to '0' while stopping the player.";
}
}

void AudioPlayer::Resume() {
if (!_isPlaying) {
_isPlaying = true;
Expand Down
2 changes: 2 additions & 0 deletions packages/audioplayers_linux/linux/audio_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class AudioPlayer {

void Pause();

void Stop();

void Resume();

void Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ static void audioplayers_linux_plugin_handle_method_call(
} else if (strcmp(method, "resume") == 0) {
player->Resume();
} else if (strcmp(method, "stop") == 0) {
player->Pause();
player->SetPosition(0);
player->Stop();
} else if (strcmp(method, "release") == 0) {
player->ReleaseMediaSource();
} else if (strcmp(method, "seek") == 0) {
Expand Down

0 comments on commit fd33b1d

Please sign in to comment.