From 4efad6e3d71e7e3ebac049569b7a82259c768480 Mon Sep 17 00:00:00 2001 From: yuruxuan <544324974@qq.com> Date: Sun, 30 Apr 2023 21:02:21 +0800 Subject: [PATCH] Fix shuffle always starts on first song(issues:#61) and remove redundant code --- .../album_screen_content_flexible_space_bar.dart | 3 +++ lib/services/audio_service_helper.dart | 16 ++++++++-------- lib/services/music_player_background_task.dart | 1 - 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/components/AlbumScreen/album_screen_content_flexible_space_bar.dart b/lib/components/AlbumScreen/album_screen_content_flexible_space_bar.dart index ef32a06af..e5e4cf012 100644 --- a/lib/components/AlbumScreen/album_screen_content_flexible_space_bar.dart +++ b/lib/components/AlbumScreen/album_screen_content_flexible_space_bar.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:get_it/get_it.dart'; @@ -70,6 +72,7 @@ class AlbumScreenContentFlexibleSpaceBar extends StatelessWidget { audioServiceHelper.replaceQueueWithItem( itemList: items, shuffle: true, + initialIndex: Random().nextInt(items.length), ), icon: const Icon(Icons.shuffle), label: Text( diff --git a/lib/services/audio_service_helper.dart b/lib/services/audio_service_helper.dart index 34265f5b0..989ee97a3 100644 --- a/lib/services/audio_service_helper.dart +++ b/lib/services/audio_service_helper.dart @@ -40,14 +40,14 @@ class AudioServiceHelper { } } - if (!shuffle) { - // Give the audio service our next initial index so that playback starts - // at that index. We don't do this if shuffling because it causes the - // queue to always start at the start (although you could argue that we - // still should if initialIndex is not 0, but that doesn't happen - // anywhere in this app so oh well). - _audioHandler.setNextInitialIndex(initialIndex); - } + // if (!shuffle) { + // // Give the audio service our next initial index so that playback starts + // // at that index. We don't do this if shuffling because it causes the + // // queue to always start at the start (although you could argue that we + // // still should if initialIndex is not 0, but that doesn't happen + // // anywhere in this app so oh well). + _audioHandler.setNextInitialIndex(initialIndex); + // } await _audioHandler.updateQueue(queue); diff --git a/lib/services/music_player_background_task.dart b/lib/services/music_player_background_task.dart index e6231075c..72a071a33 100644 --- a/lib/services/music_player_background_task.dart +++ b/lib/services/music_player_background_task.dart @@ -312,7 +312,6 @@ class MusicPlayerBackgroundTask extends BaseAudioHandler { try { switch (shuffleMode) { case AudioServiceShuffleMode.all: - await _player.shuffle(); await _player.setShuffleModeEnabled(true); shuffleNextQueue = true; break;