From 902b44fc0762f42491726281d9172fc44f9b096e Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Tue, 9 Sep 2025 16:47:59 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=AF=9D=E7=BF=BB=E8=AF=91=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E9=80=80=E5=87=BA=E5=87=BA=E7=8E=B0=E5=A5=94=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../azure_speech/AzureAsrToAsr.kt | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrToAsr.kt b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrToAsr.kt index bc154fe05..9a7c42fc4 100644 --- a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrToAsr.kt +++ b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrToAsr.kt @@ -1090,12 +1090,28 @@ fun getSupportedLanguages(): List { try { Log.d(TAG, "开始清理服务资源") - // 停止连续识别 - if (serviceState.isRecognizing.get()) { - stopContinuousTranslation() + // 停止连续识别(避免调用异步 stopContinuousTranslation 后立刻 reset 导致被取消) + runCatching { serviceState.isRecognizing.set(false) } + try { + recognizer?.stopContinuousRecognitionAsync() + ?.get(1500, java.util.concurrent.TimeUnit.MILLISECONDS) + } catch (t: Throwable) { + Log.w(TAG, "清理阶段停止识别器超时或失败", t) } - // 添加协程清理 + // 停止录音线程(打断并短等待退出) + runCatching { audioProcessor?.stopRecording() } + .onFailure { Log.w(TAG, "清理阶段停止录音失败", it) } + + // 关闭录音文件(若有) + runCatching { recordfile1?.closeFile(true) } + .onFailure { Log.w(TAG, "清理阶段关闭录音文件失败", it) } + + // 清理翻译/合成状态位(不额外等待,避免阻塞主线程) + serviceState.isTranslating.set(false) + serviceState.isSynthesizing.set(false) + + // 添加协程清理(放在资源停止之后,避免取消正在进行的停止协程) resetCoroutineScope() // 清理识别器