From a7bcaf5e433e68763f5989f29914e622d988e24f Mon Sep 17 00:00:00 2001 From: tanlongsheng <252620078@qq.com> Date: Sun, 6 Apr 2025 18:04:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=92=AD=E6=94=BE=E9=9F=B3?= =?UTF-8?q?=E4=B9=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/controllers/chat_controller.dart | 20 ++++++--- lib/modules/chat/views/chat_music_card.dart | 45 +++++++++++++------ 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/lib/modules/chat/controllers/chat_controller.dart b/lib/modules/chat/controllers/chat_controller.dart index e92c85538..77c4e030b 100644 --- a/lib/modules/chat/controllers/chat_controller.dart +++ b/lib/modules/chat/controllers/chat_controller.dart @@ -73,7 +73,7 @@ class ChatController extends GetxController { // 记录最后一次活动时间 DateTime _lastActivityTime = DateTime.now(); - final _player = AudioPlayer(); + final player = AudioPlayer(); final RxString audiourl = ''.obs; final RxBool isPlay = false.obs; @@ -157,6 +157,14 @@ class ChatController extends GetxController { // 订阅VoiceInteractionService事件 _subscribeToVoiceInteractionEvents(); + player.playerStateStream.listen((state) async { + if (state.processingState == ProcessingState.completed) { + await player.seek(Duration.zero); + player.pause(); + isPlay.value = false; + } + }); + // 使用微任务在布局完成后自动滚动到底部 WidgetsBinding.instance.addPostFrameCallback((_) { _scrollToBottom(); @@ -253,7 +261,7 @@ class ChatController extends GetxController { _historyService.saveHistory(agent.id!, messages.toList()); } - _player.dispose(); + player.dispose(); super.onClose(); } @@ -443,10 +451,10 @@ class ChatController extends GetxController { isPlay.value = false; if (audiourl.value != musicaudio['audiourl']) { audiourl.value = musicaudio['audiourl']; - await _player.setUrl(musicaudio['audiourl']); - _player.play(); + await player.setUrl(musicaudio['audiourl']); + player.play(); } else { - _player.play(); + player.play(); } isPlay.value = true; } catch (e) { @@ -458,7 +466,7 @@ class ChatController extends GetxController { // 暂停播放音乐 void pause() async { - await _player.pause(); + await player.pause(); isPlay.value = false; } diff --git a/lib/modules/chat/views/chat_music_card.dart b/lib/modules/chat/views/chat_music_card.dart index 0ae512a42..9cade7e3e 100644 --- a/lib/modules/chat/views/chat_music_card.dart +++ b/lib/modules/chat/views/chat_music_card.dart @@ -49,21 +49,38 @@ class ChatMusicCard extends GetView { onTap: () { controller.pause(); }, - child: Container( - width: 38.w, - height: 38.w, - decoration: BoxDecoration( - shape: BoxShape.circle, - border: Border.all( - color: - isDarkMode ? Colors.grey[700]! : Colors.grey[300]!, - width: 2, + child: Stack( + alignment: Alignment.center, + children: [ + SizedBox( + width: 38.w, + height: 38.w, + child: StreamBuilder( + stream: controller.player.positionStream, + builder: (context, snapshot) { + final position = snapshot.data ?? Duration.zero; + final total = + controller.player.duration ?? Duration.zero; + final progress = total.inSeconds > 0 + ? position.inSeconds / total.inSeconds + : 0.0; + return CircularProgressIndicator( + value: progress, + backgroundColor: isDarkMode + ? Colors.grey[700]! + : Colors.grey[300]!, + valueColor: AlwaysStoppedAnimation( + isDarkMode ? Colors.white : Colors.black), + strokeWidth: 2, // 线条粗细 + ); + }, + ), ), - ), - child: Icon( - Icons.pause_rounded, - color: isDarkMode ? Colors.white : Colors.black, - ), + Icon( + Icons.pause_rounded, + color: isDarkMode ? Colors.white : Colors.black, + ), + ], ), ) : GestureDetector(