Browse Source

上传音乐代码

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
c9146f571e
  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);
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) {
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 {
await _spotifService.initialize();
_spotifService.play(card['url'] as String);
@ -517,7 +544,7 @@ class ChatController extends GetxController {
}
// 暂停播放音乐
void pause() async {
void pause_spotify() async {
try {
_spotifService.pause();
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';
class ChatMusicCard extends GetView<ChatController> {
final int type;
final Map? data;
final bool isDarkMode;
const ChatMusicCard(this.data, this.isDarkMode, {super.key});
const ChatMusicCard(this.type, this.data, this.isDarkMode, {super.key});
@override
Widget build(BuildContext context) {
@ -47,7 +48,15 @@ class ChatMusicCard extends GetView<ChatController> {
controller.isPlay.value
? GestureDetector(
onTap: () {
controller.pause();
switch (type) {
case 0:
controller.pause_music();
break;
case 1:
controller.pause_spotify();
break;
default:
}
},
child: Stack(
alignment: Alignment.center,
@ -61,7 +70,15 @@ class ChatMusicCard extends GetView<ChatController> {
)
: GestureDetector(
onTap: () {
controller.play(data!);
switch (type) {
case 0:
controller.play_music(data!);
break;
case 1:
controller.play_spotify(data!);
break;
default:
}
},
child: Container(
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) {
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) {
box = ChatMusicCard(message.meta?['card_spotify'], isDarkMode);
box = ChatMusicCard(1, message.meta?['card_spotify'], isDarkMode);
}
if (message.meta?['card_navigation'] != null) {
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 {
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 {
eventSink?.success(mapOf("type" to "complete"))
}

Loading…
Cancel
Save