Browse Source

fix(音乐播放): 添加加载状态检查,防止播放状态被多次重置

在音乐播放相关方法中添加 isLoadingMusic 状态检查,防止重复点击导致的状态混乱。同时将播放操作改为异步并确保加载状态被正确重置。
weicu
lxm 1 year ago
parent
commit
b970ac317d
  1. 5
      lib/modules/agent/controllers/agent_controller.dart
  2. 83
      lib/modules/music/views/music_playlist_view.dart

5
lib/modules/agent/controllers/agent_controller.dart

@ -1568,6 +1568,11 @@ class AgentController extends GetxController {
Future<void> updateMusicProgress(int currentIdInternal, int newPosition, Future<void> updateMusicProgress(int currentIdInternal, int newPosition,
int newDuration, double newProgress) async { int newDuration, double newProgress) async {
try { try {
// 如果正在加载音乐,避免进度回调干扰状态更新
if (isLoadingMusic.value) {
return;
}
if (currentIdInternal != 0) { if (currentIdInternal != 0) {
isPlaying.value = true; isPlaying.value = true;
// return; // 如果没有在播放,直接返回 // return; // 如果没有在播放,直接返回

83
lib/modules/music/views/music_playlist_view.dart

@ -228,8 +228,8 @@ class MusicPlaylistView extends StatelessWidget {
}; };
return GestureDetector( return GestureDetector(
onTap: () { onTap: () async {
_playSelectedSong(controller, song, Get.arguments ?? []); await _playSelectedSong(controller, song, Get.arguments ?? []);
}, },
child: Container( child: Container(
margin: EdgeInsets.only(bottom: 10.h), margin: EdgeInsets.only(bottom: 10.h),
@ -512,7 +512,7 @@ class MusicPlaylistView extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
IconButton( IconButton(
onPressed: () => _playPrevious(controller, playlist), onPressed: () async => await _playPrevious(controller, playlist),
iconSize: 32.w, iconSize: 32.w,
padding: EdgeInsets.all(8.w), // 增大触摸区域 padding: EdgeInsets.all(8.w), // 增大触摸区域
icon: Icon( icon: Icon(
@ -548,7 +548,7 @@ class MusicPlaylistView extends StatelessWidget {
), ),
SizedBox(width: 16.w), SizedBox(width: 16.w),
IconButton( IconButton(
onPressed: () => _playNext(controller, playlist), onPressed: () async => await _playNext(controller, playlist),
iconSize: 32.w, iconSize: 32.w,
padding: EdgeInsets.all(8.w), // 增大触摸区域 padding: EdgeInsets.all(8.w), // 增大触摸区域
icon: Icon( icon: Icon(
@ -580,26 +580,71 @@ class MusicPlaylistView extends StatelessWidget {
} }
// 播放上一曲 // 播放上一曲
void _playPrevious( Future<void> _playPrevious(
AgentController controller, List<Map<String, dynamic>> playlist) { AgentController controller, List<Map<String, dynamic>> playlist) async {
controller.updateCurrentPlaylistContext( // 防止重复点击导致的状态混乱
playlist, controller.currentId.value); if (controller.isLoadingMusic.value) {
controller.playPrevious(); return;
}
try {
// 设置加载状态,防止重复操作
controller.isLoadingMusic.value = true;
// 更新播放列表上下文
controller.updateCurrentPlaylistContext(
playlist, controller.currentId.value);
// 执行上一曲操作
await controller.playPrevious();
} finally {
// 确保加载状态被重置
controller.isLoadingMusic.value = false;
}
} }
// 播放下一曲 // 播放下一曲
void _playNext( Future<void> _playNext(
AgentController controller, List<Map<String, dynamic>> playlist) { AgentController controller, List<Map<String, dynamic>> playlist) async {
controller.updateCurrentPlaylistContext( // 防止重复点击导致的状态混乱
playlist, controller.currentId.value); if (controller.isLoadingMusic.value) {
controller.playNext(); return;
}
try {
// 设置加载状态,防止重复操作
controller.isLoadingMusic.value = true;
// 更新播放列表上下文
controller.updateCurrentPlaylistContext(
playlist, controller.currentId.value);
// 执行下一曲操作
await controller.playNext();
} finally {
// 确保加载状态被重置
controller.isLoadingMusic.value = false;
}
} }
// 播放选中的歌曲 // 播放选中的歌曲
void _playSelectedSong(AgentController controller, Map<String, dynamic> song, Future<void> _playSelectedSong(AgentController controller, Map<String, dynamic> song,
List<Map<String, dynamic>> playlist) { List<Map<String, dynamic>> playlist) async {
final songId = song['id'] ?? 0; // 防止重复点击导致的状态混乱
controller.updateCurrentPlaylistContext(playlist, songId); if (controller.isLoadingMusic.value) {
controller.musicPlay(song); return;
}
try {
// 设置加载状态,防止重复操作
controller.isLoadingMusic.value = true;
final songId = song['id'] ?? 0;
controller.updateCurrentPlaylistContext(playlist, songId);
await controller.musicPlay(song);
} finally {
// 确保加载状态被重置
controller.isLoadingMusic.value = false;
}
} }
} }

Loading…
Cancel
Save