From c3cf6b9a9b29f181b6833b88c51a02fa28b49084 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Sat, 28 Jun 2025 17:38:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E4=B8=8A=E5=B1=82=E9=80=80?= =?UTF-8?q?=E5=87=BA=E7=95=8C=E9=9D=A2=E6=97=B6=E5=85=B3=E9=97=ADAI?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/modules/agent/controllers/agent_controller.dart | 1 + .../com/yunqiinnovation/agent_service/AgentService.kt | 9 ++++----- .../agent_service/AgentServicePlugin.kt | 10 ++++++++++ local_plugins/agent_service/lib/agent_service.dart | 10 ++++++++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/modules/agent/controllers/agent_controller.dart b/lib/modules/agent/controllers/agent_controller.dart index 3648b0639..da450222b 100644 --- a/lib/modules/agent/controllers/agent_controller.dart +++ b/lib/modules/agent/controllers/agent_controller.dart @@ -152,6 +152,7 @@ class AgentController extends GetxController { @override void onClose() { + AgentService.stopAiSteam(); //停止AI流 AgentService.stopConversation(); // 停止当前会话 AgentService.stopTts(); // 停止当前语音 textController.dispose(); diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt index fdd51c709..5a78673c6 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt @@ -571,7 +571,7 @@ object AgentService : CoroutineScope { /** * 停止AI流输出 */ - private fun stopAiStream() { + fun stopAiStream() { if (isAiStreaming) { try { // 先更新状态,避免回调时的状态不一致 @@ -580,7 +580,8 @@ object AgentService : CoroutineScope { // 取消当前AI生成任务 currentAiJob?.cancel() currentAiJob = null - + currsessionId = "" //取消会话 + // 通知ChatAPI服务终止当前流式请求 chatApiService.cancelCurrentStream() @@ -756,9 +757,6 @@ object AgentService : CoroutineScope { responseBuilder.append(token) if (speakResponse && broadcast) { ttsService?.speakStream(token) - if (token.length > 0) { - audioPlayer?.stopAudio() - } } if (broadcast) { // 发送流式回复token @@ -774,6 +772,7 @@ object AgentService : CoroutineScope { // 视情况决定是否朗读回复 if (speakResponse && broadcast && sessionid == currsessionId){ ttsService?.flushStream() + audioPlayer?.stopAudio() } val response = responseBuilder.toString() // 发送完整回复,包含是否有图片的标记 diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt index 64353c834..44f6013e8 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt @@ -177,6 +177,16 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream result.error("STOP_TTS_ERROR", "停止语音合成失败: ${e.message}", null) } } + "stopAiSteam" -> { + try { + AgentService.stopAiStream() + result.success(true) + } catch (e: Exception) { + Log.e(TAG, "停止语音合成失败", e) + result.error("STOP_TTS_ERROR", "停止语音合成失败: ${e.message}", null) + } + } + "clearChatHistory" -> { try { AgentService.clearChatHistory { success -> diff --git a/local_plugins/agent_service/lib/agent_service.dart b/local_plugins/agent_service/lib/agent_service.dart index fc1579a05..61cf6b5b1 100644 --- a/local_plugins/agent_service/lib/agent_service.dart +++ b/local_plugins/agent_service/lib/agent_service.dart @@ -293,6 +293,16 @@ class AgentService { } } + /// 停止语音合成 + static Future stopAiSteam() async { + try { + final bool result = await _channel.invokeMethod('stopAiSteam'); + return result; + } on PlatformException catch (e) { + throw AgentServiceException(e.code, e.message ?? '停止语音合成失败', e.details); + } + } + /// 停止语音合成 static Future stopTts() async { try {