diff --git a/lib/modules/agent/controllers/agent_controller.dart b/lib/modules/agent/controllers/agent_controller.dart index 03bce6c62..0a003e349 100644 --- a/lib/modules/agent/controllers/agent_controller.dart +++ b/lib/modules/agent/controllers/agent_controller.dart @@ -155,6 +155,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 ad17ede21..7a9e15d16 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 @@ -572,7 +572,7 @@ object AgentService : CoroutineScope { /** * 停止AI流输出 */ - private fun stopAiStream() { + fun stopAiStream() { if (isAiStreaming) { try { // 先更新状态,避免回调时的状态不一致 @@ -581,7 +581,8 @@ object AgentService : CoroutineScope { // 取消当前AI生成任务 currentAiJob?.cancel() currentAiJob = null - + currsessionId = "" //取消会话 + // 通知ChatAPI服务终止当前流式请求 chatApiService.cancelCurrentStream() @@ -757,9 +758,6 @@ object AgentService : CoroutineScope { responseBuilder.append(token) if (speakResponse && broadcast) { ttsService?.speakStream(token) - if (token.length > 0) { - audioPlayer?.stopAudio() - } } if (broadcast) { // 发送流式回复token @@ -775,6 +773,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 {