From 365e5e3e7bf32c26c22be25ccf9a1d7f641054d4 Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Fri, 8 Aug 2025 20:13:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=87=E6=8D=A2=E8=AF=AD?= =?UTF-8?q?=E8=A8=80=E5=94=A4=E9=86=92=E4=B8=8D=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent_service/AgentService.kt | 30 +++++++++++++++---- .../agent_service/AgentServiceImpl.swift | 15 ++++++---- 2 files changed, 34 insertions(+), 11 deletions(-) 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 d084b19fc..13594ed36 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 @@ -1134,13 +1134,31 @@ object AgentService : CoroutineScope { Log.i(TAG, "设置支持的语言: ${languages.joinToString(", ")}") // 重新初始化ASR服务以应用新的语言设置 - val asrSuccess = azureAsrHelper?.initialize( - subscriptionKey = this.azureSpeechKey, - region = this.azureSpeechRegion, - supportedLanguages = languages.toTypedArray(), - audioSourceType = audioSourceType - ) ?: false + val asrSuccess = AzureAsrHelper(context).apply { + // 使用协程处理异步初始化 + CoroutineScope(Dispatchers.IO).launch { + //获取语音识别的语种 + val supportedLanguagesConfig = config["supportedLanguages"] as? List + val supportedLanguages = supportedLanguagesConfig?.toTypedArray() + ?: arrayOf("zh-CN") // 默认支持多语言 + language = supportedLanguages[0] + val asrInitSuccess = initialize( + subscriptionKey = config["azureSpeechKey"]?.toString() ?: "", + region = config["azureSpeechRegion"]?.toString() ?: "", + supportedLanguages = supportedLanguages, + audioSourceType = if (isExternalActive) AzureAsrHelper.AudioSourceType.EXTERNAL + else AzureAsrHelper.AudioSourceType.MICROPHONE + ) + if (asrInitSuccess) { + withContext(Dispatchers.Main) { + recognizeCallback() + } + } else { + Log.w(TAG, "Azure ASR初始化失败") + } + } + } // 同时更新TTS语言设置 val ttsSuccess = updateTtsLanguage(languages.first()) // 使用第一个语言作为TTS语言 diff --git a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift index b6a154a9f..e5ce57159 100644 --- a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift +++ b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift @@ -932,16 +932,21 @@ private func jsonToString(_ json: [String: Any]) -> String? { // 重新初始化ASR引擎,使用新的语言设置 guard let asrSuccess = azureAsrHelper?.initialize( - subscriptionKey: azureSpeechKey, - region: azureSpeechRegion, + subscriptionKey: azureSpeechKey, + region: azureSpeechRegion, supportedLanguages: languages, audioSourceType: .microphone ), asrSuccess else { - os_log("重新初始化ASR引擎失败", log: logger, type: .error) - sendError("重新初始化ASR引擎失败", code: "ASR_INIT_ERROR") + sendError("初始化语音识别服务失败", code: "ASR_INIT_ERROR") return false } - + guard let asrsetupSuccess = azureAsrHelper?.setupEventListeners( + callback: self + ), asrsetupSuccess else { + sendError("初始化语音识别服务失败", code: "ASR_INIT_ERROR") + return false + } + // 同时更新TTS语言设置 let ttsSuccess = updateTtsLanguage(languages.first ?? "zh-CN")