Browse Source

上传音乐代码

weicu
liwei1dao 1 year ago
parent
commit
2b85c00260
  1. 33
      lib/modules/chat/controllers/chat_controller.dart
  2. 23
      lib/modules/chat/views/chat_music_card.dart
  3. 5
      lib/modules/chat/views/chat_view.dart
  4. 2
      local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/OpenAIServicePlugin.kt

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

@ -418,7 +418,10 @@ class ChatController extends GetxController {
); );
_updateIntermediateMessage(meta: chunk); _updateIntermediateMessage(meta: chunk);
if (chunk['card_spotify'] != null) { if (chunk['card_spotify'] != null) {
play(chunk['card_spotify']); play_spotify(chunk['card_spotify']);
}
if (chunk['card_music'] != null) {
play_music(chunk['card_music']);
} }
} else if (chunk is String) { } else if (chunk is String) {
fullResponse += chunk; fullResponse += chunk;
@ -505,7 +508,31 @@ class ChatController extends GetxController {
} }
// 播放音乐 // 播放音乐
void play(Map card) async { void play_music(Map card) async {
try {
isPlay.value = false;
if (audiourl.value != card['url']) {
audiourl.value = '';
await player.setUrl(card['url']);
audiourl.value = card['url'];
player.play();
} else {
player.play();
}
isPlay.value = true;
} catch (e) {
Logger.error('加载异常: $e');
}
}
// 播放音乐
void pause_music() async {
await player.pause();
isPlay.value = false;
}
// 播放音乐
void play_spotify(Map card) async {
try { try {
await _spotifService.initialize(); await _spotifService.initialize();
_spotifService.play(card['url'] as String); _spotifService.play(card['url'] as String);
@ -517,7 +544,7 @@ class ChatController extends GetxController {
} }
// 暂停播放音乐 // 暂停播放音乐
void pause() async { void pause_spotify() async {
try { try {
_spotifService.pause(); _spotifService.pause();
isPlay.value = false; isPlay.value = false;

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

@ -5,10 +5,11 @@ import 'package:get/get.dart';
import '../controllers/chat_controller.dart'; import '../controllers/chat_controller.dart';
class ChatMusicCard extends GetView<ChatController> { class ChatMusicCard extends GetView<ChatController> {
final int type;
final Map? data; final Map? data;
final bool isDarkMode; final bool isDarkMode;
const ChatMusicCard(this.data, this.isDarkMode, {super.key}); const ChatMusicCard(this.type, this.data, this.isDarkMode, {super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -47,7 +48,15 @@ class ChatMusicCard extends GetView<ChatController> {
controller.isPlay.value controller.isPlay.value
? GestureDetector( ? GestureDetector(
onTap: () { onTap: () {
controller.pause(); switch (type) {
case 0:
controller.pause_music();
break;
case 1:
controller.pause_spotify();
break;
default:
}
}, },
child: Stack( child: Stack(
alignment: Alignment.center, alignment: Alignment.center,
@ -61,7 +70,15 @@ class ChatMusicCard extends GetView<ChatController> {
) )
: GestureDetector( : GestureDetector(
onTap: () { onTap: () {
controller.play(data!); switch (type) {
case 0:
controller.play_music(data!);
break;
case 1:
controller.play_spotify(data!);
break;
default:
}
}, },
child: Container( child: Container(
width: 38.w, width: 38.w,

5
lib/modules/chat/views/chat_view.dart

@ -374,8 +374,11 @@ class ChatView extends GetView<ChatController> {
if (message.meta?['card_weather'] != null) { if (message.meta?['card_weather'] != null) {
box = WeatherDayCard(message.meta?['card_weather'], isDarkMode); box = WeatherDayCard(message.meta?['card_weather'], isDarkMode);
} }
if (message.meta?['card_music'] != null) {
box = ChatMusicCard(0, message.meta?['card_music'], isDarkMode);
}
if (message.meta?['card_spotify'] != null) { if (message.meta?['card_spotify'] != null) {
box = ChatMusicCard(message.meta?['card_spotify'], isDarkMode); box = ChatMusicCard(1, message.meta?['card_spotify'], isDarkMode);
} }
if (message.meta?['card_navigation'] != null) { if (message.meta?['card_navigation'] != null) {
box = ChatNavigationCard( box = ChatNavigationCard(

2
local_plugins/open_ai_service/android/src/main/kotlin/com/yunqiinnovation/open_ai_service/OpenAIServicePlugin.kt

@ -175,7 +175,7 @@ class OpenAIServicePlugin : FlutterPlugin, MethodCallHandler, StreamHandler {
mainHandler.post { mainHandler.post {
eventSink?.success(mapOf("type" to "functionCall", "content" to context,"meta" to meta.toString())) eventSink?.success(mapOf("type" to "functionCall", "content" to context,"meta" to meta.toString()))
} }
if (metaJsonObject.containsKey("card_spotify")) { if (metaJsonObject.containsKey("card_spotify") || metaJsonObject.containsKey("card_music")) {
mainHandler.post { mainHandler.post {
eventSink?.success(mapOf("type" to "complete")) eventSink?.success(mapOf("type" to "complete"))
} }

Loading…
Cancel
Save