From effc7316ce830fb21a67abf29bd0f3df3870b978 Mon Sep 17 00:00:00 2001 From: tanlongsheng <252620078@qq.com> Date: Tue, 9 Dec 2025 20:28:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=AF=E9=A3=9E=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../azure_speech/AzureAsrHelper.kt | 4 +-- .../azure_speech/XunFeiAsrHelper.kt | 35 ++++++++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt index 3f6aaed83..793193be7 100644 --- a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt +++ b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt @@ -114,7 +114,7 @@ class AzureAsrHelper(private val context: Context) { this.supportedLanguages = supportedLanguages } - // this.useXunfei = supportedLanguages.any { it == "zh-CN" || it == "en-US" } + this.useXunfei = supportedLanguages.any { it == "zh-CN" || it == "en-US" } // 根据支持的语言数量决定是否启用自动语言检测 this.isAutoDetectLanguage = supportedLanguages.size >= 2 @@ -285,7 +285,7 @@ class AzureAsrHelper(private val context: Context) { } try { - xunFeiAsrHelper?.start(callback) + xunFeiAsrHelper?.start(callback, currentLanguage, isAutoDetectLanguage) this.audioSourceType = audioSourceType if (!audioStream.isContinuousRecognitionActive) { diff --git a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt index 491c3dbaf..7a5450452 100644 --- a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt +++ b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt @@ -27,7 +27,10 @@ class XunFeiAsrHelper(private val context: Context) { private var sessionId = "" private var lastIntermediateResult = "" - fun start(callback: AzureAsrHelper.ContinuousRecognizeCallback) { + private var currentLanguage = "zh-CN" + private var isAutoDetectLanguage = false + + fun start(callback: AzureAsrHelper.ContinuousRecognizeCallback, language: String, isAutoDetect: Boolean) { try { webSocket?.cancel() webSocket = null @@ -38,6 +41,9 @@ class XunFeiAsrHelper(private val context: Context) { currentSessionId = UUID.randomUUID().toString() lastIntermediateResult = "" + currentLanguage = language + isAutoDetectLanguage = isAutoDetect + val url = getWebSocketUrl() val request = Request.Builder().url(url).build() @@ -59,8 +65,17 @@ class XunFeiAsrHelper(private val context: Context) { override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { Log.d(tag, "WebSocket Closed: $code / $reason") if (currentSessionId.isNotEmpty() && lastIntermediateResult.isNotEmpty()) { - callback.onResult(currentSessionId, lastIntermediateResult, "zh-CN") - currentSessionId = "" + val detectedLanguage = if (isAutoDetectLanguage) { + if (lastIntermediateResult.contains(Regex("[\\u4e00-\\u9fa5]"))) { + "zh-CN" + } else { + "en-US" + } + } else { + currentLanguage + } + callback.onResult(currentSessionId, lastIntermediateResult, detectedLanguage) + currentSessionId = "" } callback.onSessionStopped(currentSessionId) } @@ -144,8 +159,18 @@ class XunFeiAsrHelper(private val context: Context) { return } + val detectedLanguage = if (isAutoDetectLanguage) { + if (resultTextTemp.contains(Regex("[\\u4e00-\\u9fa5]"))) { + "zh-CN" + } else { + "en-US" + } + } else { + currentLanguage + } + if (st.optInt("type") == 0) { - callback.onResult(currentSessionId, resultTextTemp, "zh-CN") + callback.onResult(currentSessionId, resultTextTemp, detectedLanguage) currentSessionId = "" lastIntermediateResult = "" } else { @@ -154,7 +179,7 @@ class XunFeiAsrHelper(private val context: Context) { callback.onSessionStarted(currentSessionId) } lastIntermediateResult = resultTextTemp - callback.onRecognizing(currentSessionId, resultTextTemp, "zh-CN") + callback.onRecognizing(currentSessionId, resultTextTemp, detectedLanguage) } }