Browse Source

修改播放音乐

newdev_shunjiawei
tanlongsheng 2 years ago
parent
commit
61f7ad41ba
  1. 20
      lib/modules/chat/controllers/chat_controller.dart
  2. 45
      lib/modules/chat/views/chat_music_card.dart

20
lib/modules/chat/controllers/chat_controller.dart

@ -73,7 +73,7 @@ class ChatController extends GetxController {
// 记录最后一次活动时间 // 记录最后一次活动时间
DateTime _lastActivityTime = DateTime.now(); DateTime _lastActivityTime = DateTime.now();
final _player = AudioPlayer(); final player = AudioPlayer();
final RxString audiourl = ''.obs; final RxString audiourl = ''.obs;
final RxBool isPlay = false.obs; final RxBool isPlay = false.obs;
@ -157,6 +157,14 @@ class ChatController extends GetxController {
// 订阅VoiceInteractionService事件 // 订阅VoiceInteractionService事件
_subscribeToVoiceInteractionEvents(); _subscribeToVoiceInteractionEvents();
player.playerStateStream.listen((state) async {
if (state.processingState == ProcessingState.completed) {
await player.seek(Duration.zero);
player.pause();
isPlay.value = false;
}
});
// 使用微任务在布局完成后自动滚动到底部 // 使用微任务在布局完成后自动滚动到底部
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollToBottom(); _scrollToBottom();
@ -253,7 +261,7 @@ class ChatController extends GetxController {
_historyService.saveHistory(agent.id!, messages.toList()); _historyService.saveHistory(agent.id!, messages.toList());
} }
_player.dispose(); player.dispose();
super.onClose(); super.onClose();
} }
@ -443,10 +451,10 @@ class ChatController extends GetxController {
isPlay.value = false; isPlay.value = false;
if (audiourl.value != musicaudio['audiourl']) { if (audiourl.value != musicaudio['audiourl']) {
audiourl.value = musicaudio['audiourl']; audiourl.value = musicaudio['audiourl'];
await _player.setUrl(musicaudio['audiourl']); await player.setUrl(musicaudio['audiourl']);
_player.play(); player.play();
} else { } else {
_player.play(); player.play();
} }
isPlay.value = true; isPlay.value = true;
} catch (e) { } catch (e) {
@ -458,7 +466,7 @@ class ChatController extends GetxController {
// 暂停播放音乐 // 暂停播放音乐
void pause() async { void pause() async {
await _player.pause(); await player.pause();
isPlay.value = false; isPlay.value = false;
} }

45
lib/modules/chat/views/chat_music_card.dart

@ -49,21 +49,38 @@ class ChatMusicCard extends GetView<ChatController> {
onTap: () { onTap: () {
controller.pause(); controller.pause();
}, },
child: Container( child: Stack(
width: 38.w, alignment: Alignment.center,
height: 38.w, children: [
decoration: BoxDecoration( SizedBox(
shape: BoxShape.circle, width: 38.w,
border: Border.all( height: 38.w,
color: child: StreamBuilder<Duration>(
isDarkMode ? Colors.grey[700]! : Colors.grey[300]!, stream: controller.player.positionStream,
width: 2, 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<Color>(
isDarkMode ? Colors.white : Colors.black),
strokeWidth: 2, // 线条粗细
);
},
),
), ),
), Icon(
child: Icon( Icons.pause_rounded,
Icons.pause_rounded, color: isDarkMode ? Colors.white : Colors.black,
color: isDarkMode ? Colors.white : Colors.black, ),
), ],
), ),
) )
: GestureDetector( : GestureDetector(

Loading…
Cancel
Save