Skip to content

Commit

Permalink
feat: pause track when seeking forward/back and keep audio session al…
Browse files Browse the repository at this point in the history
…ive when paused/interrupted
  • Loading branch information
KRTirtho committed Dec 7, 2022
1 parent 3ed8b0f commit bc8a04e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/provider/Playback.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class Playback extends PersistedChangeNotifier {
if (index < 0 || index > playlist.tracks.length - 1) return;
if (isPlaying || status == PlaybackStatus.playing) await stop();
this.playlist = playlist;
mobileAudioService?.session?.setActive(true);
final played = this.playlist!.tracks[index];
status = PlaybackStatus.loading;
notifyListeners();
Expand Down Expand Up @@ -320,6 +321,7 @@ class Playback extends PersistedChangeNotifier {
}

Future<void> stop() async {
mobileAudioService?.session?.setActive(false);
await player.stop();
await player.release();
isPlaying = false;
Expand Down Expand Up @@ -634,6 +636,7 @@ class Playback extends PersistedChangeNotifier {
(playlist!.trackIds.indexOf(track!.id!) + 1).toInt();
// checking if there's any track available forward
if (nextTrackIndex > (playlist?.tracks.length ?? 0) - 1) return;
await pause();
await play(playlist!.tracks.elementAt(nextTrackIndex)).then((_) {
playlist!.tracks[nextTrackIndex] = track!;
});
Expand All @@ -645,6 +648,7 @@ class Playback extends PersistedChangeNotifier {
(playlist!.trackIds.indexOf(track!.id!) - 1).toInt();
// checking if there's any track available behind
if (prevTrackIndex < 0) return;
await pause();
await play(playlist!.tracks.elementAt(prevTrackIndex)).then((_) {
playlist!.tracks[prevTrackIndex] = track!;
});
Expand Down
9 changes: 4 additions & 5 deletions lib/services/MobileAudioService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ class MobileAudioService extends BaseAudioHandler {
}
});
});
final _player = playback.player;
_player.onPlayerStateChanged.listen((state) async {
final player = playback.player;
player.onPlayerStateChanged.listen((state) async {
if (state != PlayerState.completed) {
playbackState.add(await _transformEvent());
}
});

_player.onPositionChanged.listen((pos) async {
player.onPositionChanged.listen((pos) async {
playbackState.add(await _transformEvent());
});

_player.onPlayerComplete.listen((_) {
player.onPlayerComplete.listen((_) {
if (playback.playlist == null && playback.track == null) {
playbackState.add(
PlaybackState(
Expand All @@ -56,7 +56,6 @@ class MobileAudioService extends BaseAudioHandler {

@override
Future<void> stop() async {
await session?.setActive(true);
await playback.stop();
}

Expand Down

0 comments on commit bc8a04e

Please sign in to comment.