Skip to content

Commit 34cb4a0

Browse files
committed
feat: enhance audio playback controls with repeat mode functionality
1 parent fb1435f commit 34cb4a0

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

lib/screens/now_playing_page.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,21 @@ class NowPlayingPage extends StatelessWidget {
362362
},
363363
),
364364
const SizedBox(width: 10),
365-
IconButton(
366-
icon: Icon(
367-
FluentIcons.next_24_filled,
368-
color: audioHandler.hasNext ? _primaryColor : _secondaryColor,
369-
),
370-
iconSize: screen * 0.115,
371-
onPressed: () => audioHandler.skipToNext(),
372-
splashColor: Colors.transparent,
365+
ValueListenableBuilder<AudioServiceRepeatMode>(
366+
valueListenable: repeatNotifier,
367+
builder: (_, repeatMode, __) {
368+
return IconButton(
369+
icon: Icon(
370+
FluentIcons.next_24_filled,
371+
color: audioHandler.hasNext
372+
? _primaryColor
373+
: _secondaryColor,
374+
),
375+
iconSize: screen * 0.115,
376+
onPressed: () => audioHandler.skipToNext(),
377+
splashColor: Colors.transparent,
378+
);
379+
},
373380
),
374381
],
375382
),
@@ -390,6 +397,8 @@ class NowPlayingPage extends StatelessWidget {
390397
repeatMode == AudioServiceRepeatMode.all
391398
? AudioServiceRepeatMode.one
392399
: AudioServiceRepeatMode.none;
400+
401+
audioHandler.setRepeatMode(repeatMode);
393402
},
394403
)
395404
: IconButton.filledTonal(

lib/services/audio_service.dart

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,9 @@ class MusifyAudioHandler extends BaseAudioHandler {
332332
Future<void> skipToNext() async {
333333
if (repeatNotifier.value == AudioServiceRepeatMode.one) {
334334
// If repeat mode is set to repeat the current song, play the current song again
335-
await skipToSong(activeSongId);
335+
if (audioPlayer.playing) {
336+
await audioPlayer.seek(Duration.zero);
337+
}
336338
} else if (!hasNext && repeatNotifier.value == AudioServiceRepeatMode.all) {
337339
// If repeat mode is set to repeat the playlist, start from the beginning
338340
await skipToSong(0);
@@ -344,15 +346,24 @@ class MusifyAudioHandler extends BaseAudioHandler {
344346
} else if (hasNext) {
345347
// If there is a next song, skip to the next song
346348
await skipToSong(activeSongId + 1);
347-
} else {
348-
// Handle end of playlist without repeat
349-
await audioPlayer.stop();
350349
}
351350
}
352351

353352
@override
354353
Future<void> skipToPrevious() async {
355-
await skipToSong(activeSongId - 1);
354+
if (repeatNotifier.value == AudioServiceRepeatMode.one) {
355+
// If repeat mode is set to repeat the current song, play the current song again
356+
if (audioPlayer.playing) {
357+
await audioPlayer.seek(Duration.zero);
358+
}
359+
} else if (!hasPrevious &&
360+
repeatNotifier.value == AudioServiceRepeatMode.all) {
361+
// If repeat mode is set to repeat the playlist, start from the end
362+
await skipToSong(activePlaylist['list'].length - 1);
363+
} else if (hasPrevious) {
364+
// If there is a previous song, skip to the previous song
365+
await skipToSong(activeSongId - 1);
366+
}
356367
}
357368

358369
@override
@@ -362,6 +373,13 @@ class MusifyAudioHandler extends BaseAudioHandler {
362373
await audioPlayer.setShuffleModeEnabled(shuffleEnabled);
363374
}
364375

376+
@override
377+
Future<void> setRepeatMode(AudioServiceRepeatMode repeatMode) async {
378+
await audioPlayer.setLoopMode(
379+
repeatMode == AudioServiceRepeatMode.all ? LoopMode.one : LoopMode.off,
380+
);
381+
}
382+
365383
void changeSponsorBlockStatus() {
366384
sponsorBlockSupport.value = !sponsorBlockSupport.value;
367385
addOrUpdateData(

0 commit comments

Comments
 (0)