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; + } } }