Skip to content

Commit

Permalink
feat: use real shuffle (#1089)
Browse files Browse the repository at this point in the history
Fixes #1088
  • Loading branch information
Feichtmeier authored Dec 11, 2024
1 parent c385852 commit f1d51e0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
33 changes: 11 additions & 22 deletions lib/player/player_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';

import 'package:audio_service/audio_service.dart';
Expand Down Expand Up @@ -143,6 +142,7 @@ class PlayerService {
await _player.dispose();
}

List<Audio>? _oldQueue;
Queue _queue = (name: '', audios: []);
Queue get queue => _queue;
void setQueue(Queue value) {
Expand Down Expand Up @@ -235,8 +235,13 @@ class PlayerService {
void setShuffle(bool value) {
if (value == _shuffle) return;
_shuffle = value;
if (value && queue.audios.length > 1) {
_randomNext();
if (value) {
_oldQueue = _queue.audios;
_queue.audios.shuffle();
} else if (_oldQueue != null) {
setQueue(
(audios: _oldQueue!, name: _queue.name),
);
}
_propertiesChangedController.add(true);
}
Expand Down Expand Up @@ -374,30 +379,14 @@ class PlayerService {
if (queue.audios.isNotEmpty && queue.audios.contains(audio)) {
final currentIndex = queue.audios.indexOf(audio!);

if (shuffle && queue.audios.length > 1) {
_randomNext();
if (currentIndex == queue.audios.length - 1) {
nextAudio = queue.audios.elementAt(0);
} else {
if (currentIndex == queue.audios.length - 1) {
nextAudio = queue.audios.elementAt(0);
} else {
nextAudio = queue.audios.elementAt(queue.audios.indexOf(audio!) + 1);
}
nextAudio = queue.audios.elementAt(queue.audios.indexOf(audio!) + 1);
}
}
}

void _randomNext() {
if (audio == null) return;
final currentIndex = queue.audios.indexOf(audio!);
final max = queue.audios.length;
final random = Random();
var randomIndex = random.nextInt(max);
while (randomIndex == currentIndex) {
randomIndex = random.nextInt(max);
}
nextAudio = queue.audios.elementAt(randomIndex);
}

Future<void> playPrevious() async {
if (queue.audios.isNotEmpty == true &&
audio != null &&
Expand Down
12 changes: 7 additions & 5 deletions lib/player/view/queue_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,13 @@ class _QueueTileState extends State<_QueueTile> {
RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
onTap: widget.queueName == null
? null
: () => di<PlayerModel>().startPlaylist(
listName: widget.queueName!,
audios: widget.queue,
index: widget.queue.indexOf(widget.audio),
),
: () => di<PlayerModel>()
..setShuffle(false)
..startPlaylist(
listName: widget.queueName!,
audios: widget.queue,
index: widget.queue.indexOf(widget.audio),
),
hoverColor: context.colorScheme.onSurface.withOpacity(0.3),
leading: Visibility(
visible: widget.selected,
Expand Down

0 comments on commit f1d51e0

Please sign in to comment.