From b970ac317debac38bc3c4446d8e0915890b92d97 Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Tue, 8 Jul 2025 15:16:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=9F=B3=E4=B9=90=E6=92=AD=E6=94=BE):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=A0=E8=BD=BD=E7=8A=B6=E6=80=81=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=EF=BC=8C=E9=98=B2=E6=AD=A2=E6=92=AD=E6=94=BE=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E8=A2=AB=E5=A4=9A=E6=AC=A1=E9=87=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在音乐播放相关方法中添加 isLoadingMusic 状态检查,防止重复点击导致的状态混乱。同时将播放操作改为异步并确保加载状态被正确重置。 --- .../agent/controllers/agent_controller.dart | 5 ++ .../music/views/music_playlist_view.dart | 83 ++++++++++++++----- 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/lib/modules/agent/controllers/agent_controller.dart b/lib/modules/agent/controllers/agent_controller.dart index ab76f6703..0192f079f 100644 --- a/lib/modules/agent/controllers/agent_controller.dart +++ b/lib/modules/agent/controllers/agent_controller.dart @@ -1568,6 +1568,11 @@ class AgentController extends GetxController { Future updateMusicProgress(int currentIdInternal, int newPosition, int newDuration, double newProgress) async { try { + // 如果正在加载音乐,避免进度回调干扰状态更新 + if (isLoadingMusic.value) { + return; + } + if (currentIdInternal != 0) { isPlaying.value = true; // return; // 如果没有在播放,直接返回 diff --git a/lib/modules/music/views/music_playlist_view.dart b/lib/modules/music/views/music_playlist_view.dart index e902c46df..29f2c520b 100644 --- a/lib/modules/music/views/music_playlist_view.dart +++ b/lib/modules/music/views/music_playlist_view.dart @@ -228,8 +228,8 @@ class MusicPlaylistView extends StatelessWidget { }; return GestureDetector( - onTap: () { - _playSelectedSong(controller, song, Get.arguments ?? []); + onTap: () async { + await _playSelectedSong(controller, song, Get.arguments ?? []); }, child: Container( margin: EdgeInsets.only(bottom: 10.h), @@ -512,7 +512,7 @@ class MusicPlaylistView extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ IconButton( - onPressed: () => _playPrevious(controller, playlist), + onPressed: () async => await _playPrevious(controller, playlist), iconSize: 32.w, padding: EdgeInsets.all(8.w), // 增大触摸区域 icon: Icon( @@ -548,7 +548,7 @@ class MusicPlaylistView extends StatelessWidget { ), SizedBox(width: 16.w), IconButton( - onPressed: () => _playNext(controller, playlist), + onPressed: () async => await _playNext(controller, playlist), iconSize: 32.w, padding: EdgeInsets.all(8.w), // 增大触摸区域 icon: Icon( @@ -580,26 +580,71 @@ class MusicPlaylistView extends StatelessWidget { } // 播放上一曲 - void _playPrevious( - AgentController controller, List> playlist) { - controller.updateCurrentPlaylistContext( - playlist, controller.currentId.value); - controller.playPrevious(); + Future _playPrevious( + AgentController controller, List> playlist) async { + // 防止重复点击导致的状态混乱 + if (controller.isLoadingMusic.value) { + return; + } + + try { + // 设置加载状态,防止重复操作 + controller.isLoadingMusic.value = true; + + // 更新播放列表上下文 + controller.updateCurrentPlaylistContext( + playlist, controller.currentId.value); + + // 执行上一曲操作 + await controller.playPrevious(); + } finally { + // 确保加载状态被重置 + controller.isLoadingMusic.value = false; + } } // 播放下一曲 - void _playNext( - AgentController controller, List> playlist) { - controller.updateCurrentPlaylistContext( - playlist, controller.currentId.value); - controller.playNext(); + Future _playNext( + AgentController controller, List> playlist) async { + // 防止重复点击导致的状态混乱 + if (controller.isLoadingMusic.value) { + 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 song, - List> playlist) { - final songId = song['id'] ?? 0; - controller.updateCurrentPlaylistContext(playlist, songId); - controller.musicPlay(song); + Future _playSelectedSong(AgentController controller, Map song, + List> playlist) async { + // 防止重复点击导致的状态混乱 + if (controller.isLoadingMusic.value) { + return; + } + + try { + // 设置加载状态,防止重复操作 + controller.isLoadingMusic.value = true; + + final songId = song['id'] ?? 0; + controller.updateCurrentPlaylistContext(playlist, songId); + await controller.musicPlay(song); + } finally { + // 确保加载状态被重置 + controller.isLoadingMusic.value = false; + } } }