From 588ddf068c158ea134abeabf159bd2e6b2df47e6 Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Wed, 3 Sep 2025 12:38:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=9A=E8=AF=9D=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=EF=BC=8C=E7=BB=93=E6=9D=9F=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E5=9C=A8=E8=AF=B4=E8=AF=9D=EF=BC=8C=E5=AF=BC=E8=87=B4=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E7=BB=93=E6=9E=9C=E8=BF=9B=E5=85=A5=E4=BA=86=E7=BF=BB?= =?UTF-8?q?=E8=AF=91=E5=BC=95=E5=8F=91=E5=8D=A1=E6=AD=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../azure_speech/AzureAsrToAsr.kt | 21 ++++++++++++++----- .../IntegratedSpeechTranslationService.swift | 17 ++++++++++++--- 2 files changed, 30 insertions(+), 8 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 c04bc32cb..f2b170bdf 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 @@ -328,10 +328,14 @@ class IntegratedSpeechTranslationService( confidence ) Log.d(TAG, "识别完成事件: ${event.result.text},serviceConfig.sourceLanguage:${serviceConfig.sourceLanguage}") - // 触发翻译流程 - launch { - Log.d(TAG, "触发翻译流程: ${event.result.text}") - processTranslationAndSynthesis(event.result.text) + // 只有在服务仍在运行时才触发翻译流程 + if (serviceState.isRecognizing.get()) { + launch { + Log.d(TAG, "触发翻译流程: ${event.result.text}") + processTranslationAndSynthesis(event.result.text) + } + } else { + Log.d(TAG, "服务已停止,跳过翻译流程: ${event.result.text}") } } } @@ -478,6 +482,7 @@ class IntegratedSpeechTranslationService( recognizer?.stopContinuousRecognitionAsync() audioProcessor?.stopRecording() recordfile1!!.closeFile(true) + serviceState.isRecognizing.set(false) // 等待当前处理完成 while (serviceState.isTranslating.get() || serviceState.isSynthesizing.get()) { Thread.sleep(100) @@ -494,7 +499,13 @@ class IntegratedSpeechTranslationService( * 处理翻译和合成流程 */ private suspend fun processTranslationAndSynthesis(text: String) { - Log.d(TAG, " 翻译状态:${serviceState.isTranslating.get()}") + // 检查服务是否仍在运行 + if (!serviceState.isRecognizing.get()) { + Log.d(TAG, "服务已停止,取消翻译流程: $text") + return + } + + Log.d(TAG, " 翻译状态:${serviceState.isTranslating.get()}") if (serviceState.isTranslating.get()) { Log.w(TAG, "翻译正在进行中,跳过当前请求") return diff --git a/local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/IntegratedSpeechTranslationService.swift b/local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/IntegratedSpeechTranslationService.swift index db83bd553..70d8773bd 100644 --- a/local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/IntegratedSpeechTranslationService.swift +++ b/local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/IntegratedSpeechTranslationService.swift @@ -388,9 +388,13 @@ import os.log os_log("识别完成事件: %@", log: self.log, type: .info, text) - // 触发翻译流程 - Task { - await self.processTranslationAndSynthesis(text: text) + // 检查服务状态,只有在识别仍然活跃时才触发翻译流程 + if self.serviceState.isRecognizing { + Task { + await self.processTranslationAndSynthesis(text: text) + } + } else { + os_log("服务已停止,跳过翻译流程", log: self.log, type: .info) } } @@ -593,6 +597,7 @@ import os.log */ public func stopContinuousTranslation() { do { + self.serviceState.isRecognizing = false try recognizer?.stopContinuousRecognition() audioProcessor?.stopMicrophoneCapture() recordFile?.closeFile(isSave: true) @@ -614,6 +619,12 @@ import os.log * 处理翻译和合成流程 */ private func processTranslationAndSynthesis(text: String) async { + // 添加服务状态检查 + guard serviceState.isRecognizing else { + os_log("服务已停止,取消翻译流程", log: log, type: .info) + return + } + os_log("翻译状态: %@", log: log, type: .debug, serviceState.isTranslating ? "进行中" : "空闲") if serviceState.isTranslating {