Browse Source

修改播放音乐

weicu
tanlongsheng 2 years ago
parent
commit
a7bcaf5e43
  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();
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;
}

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

@ -49,21 +49,38 @@ class ChatMusicCard extends GetView<ChatController> {
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<Duration>(
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<Color>(
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(

Loading…
Cancel
Save