Browse Source

修复通话翻译,结束的时候在说话,导致识别结果进入了翻译引发卡死

newdev_shunjiawei
fdp 1 year ago
parent
commit
588ddf068c
  1. 21
      local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrToAsr.kt
  2. 17
      local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/IntegratedSpeechTranslationService.swift

21
local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrToAsr.kt

@ -328,10 +328,14 @@ class IntegratedSpeechTranslationService(
confidence confidence
) )
Log.d(TAG, "识别完成事件: ${event.result.text},serviceConfig.sourceLanguage:${serviceConfig.sourceLanguage}") Log.d(TAG, "识别完成事件: ${event.result.text},serviceConfig.sourceLanguage:${serviceConfig.sourceLanguage}")
// 触发翻译流程 // 只有在服务仍在运行时才触发翻译流程
launch { if (serviceState.isRecognizing.get()) {
Log.d(TAG, "触发翻译流程: ${event.result.text}") launch {
processTranslationAndSynthesis(event.result.text) 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() recognizer?.stopContinuousRecognitionAsync()
audioProcessor?.stopRecording() audioProcessor?.stopRecording()
recordfile1!!.closeFile(true) recordfile1!!.closeFile(true)
serviceState.isRecognizing.set(false)
// 等待当前处理完成 // 等待当前处理完成
while (serviceState.isTranslating.get() || serviceState.isSynthesizing.get()) { while (serviceState.isTranslating.get() || serviceState.isSynthesizing.get()) {
Thread.sleep(100) Thread.sleep(100)
@ -494,7 +499,13 @@ class IntegratedSpeechTranslationService(
* 处理翻译和合成流程 * 处理翻译和合成流程
*/ */
private suspend fun processTranslationAndSynthesis(text: String) { 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()) { if (serviceState.isTranslating.get()) {
Log.w(TAG, "翻译正在进行中,跳过当前请求") Log.w(TAG, "翻译正在进行中,跳过当前请求")
return return

17
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) os_log("识别完成事件: %@", log: self.log, type: .info, text)
// 触发翻译流程 // 检查服务状态,只有在识别仍然活跃时才触发翻译流程
Task { if self.serviceState.isRecognizing {
await self.processTranslationAndSynthesis(text: text) Task {
await self.processTranslationAndSynthesis(text: text)
}
} else {
os_log("服务已停止,跳过翻译流程", log: self.log, type: .info)
} }
} }
@ -593,6 +597,7 @@ import os.log
*/ */
public func stopContinuousTranslation() { public func stopContinuousTranslation() {
do { do {
self.serviceState.isRecognizing = false
try recognizer?.stopContinuousRecognition() try recognizer?.stopContinuousRecognition()
audioProcessor?.stopMicrophoneCapture() audioProcessor?.stopMicrophoneCapture()
recordFile?.closeFile(isSave: true) recordFile?.closeFile(isSave: true)
@ -614,6 +619,12 @@ import os.log
* 处理翻译和合成流程 * 处理翻译和合成流程
*/ */
private func processTranslationAndSynthesis(text: String) async { 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 ? "进行中" : "空闲") os_log("翻译状态: %@", log: log, type: .debug, serviceState.isTranslating ? "进行中" : "空闲")
if serviceState.isTranslating { if serviceState.isTranslating {

Loading…
Cancel
Save