Browse Source

fix(azure_speech): 将异步语音合成方法改为同步以提高稳定性

修改Azure语音合成方法,从异步调用改为同步调用,避免潜在的并发问题。同时更新版本号以反映此次修改。
newdev_shunjiawei
lxm 1 year ago
parent
commit
cb01d07bd7
  1. 41
      local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureTtsHelper.swift
  2. 2
      pubspec.yaml

41
local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureTtsHelper.swift

@ -276,11 +276,9 @@ public class AzureTtsHelper: NSObject, ITtsService {
do { do {
print("liwei-------------停止语音合成播放") print("liwei-------------停止语音合成播放")
// 使用异步停止方法 // 使用同步停止方法
if let synthesizer = self.synthesizer { if let synthesizer = self.synthesizer {
let stopFuture = synthesizer.stopSpeakingAsync() try synthesizer.stopSpeaking()
// 等待停止操作完成
_ = stopFuture.get()
} }
self.notifyEvent(eventType: .synthesisCanceled) self.notifyEvent(eventType: .synthesisCanceled)
@ -328,8 +326,7 @@ public class AzureTtsHelper: NSObject, ITtsService {
// 确保合成器完全停止后再销毁 // 确保合成器完全停止后再销毁
if let synthesizer = self.synthesizer { if let synthesizer = self.synthesizer {
do { do {
let stopFuture = synthesizer.stopSpeakingAsync() try synthesizer.stopSpeaking()
_ = try stopFuture.get()
} catch { } catch {
os_log("停止合成器失败: %{public}@", log: self.log, type: .error, error.localizedDescription) os_log("停止合成器失败: %{public}@", log: self.log, type: .error, error.localizedDescription)
} }
@ -493,28 +490,18 @@ public class AzureTtsHelper: NSObject, ITtsService {
} }
os_log("开始合成: %{public}@", log: log, type: .debug, ssml) os_log("开始合成: %{public}@", log: log, type: .debug, ssml)
// 使用异步合成方法 // 使用同步合成方法
if let synthesizer = self.synthesizer { if let synthesizer = self.synthesizer {
let future = synthesizer.speakSsmlAsync(ssml) do {
try synthesizer.startSpeakingSsml(ssml)
// 在后台等待合成完成 os_log("合成开始成功", log: self.log, type: .debug)
DispatchQueue.global(qos: .userInitiated).async { [weak self] in } catch {
defer { self.speaking = false
self?.synthesisGroup.leave() os_log("合成失败: %{public}@", log: self.log, type: .error, error.localizedDescription)
} self.notifyEvent(eventType: .error, params: [
"errorCode": "SYNTHESIS_FAILED",
do { "errorMessage": error.localizedDescription
let result = try future.get() ])
os_log("合成完成,结果: %{public}@", log: self?.log ?? OSLog.default, type: .debug, String(describing: result.reason))
} catch {
self?.speaking = false
self?.pendingTextCount = max(0, (self?.pendingTextCount ?? 0) - text.count)
os_log("异步语音合成失败: %{public}@", log: self?.log ?? OSLog.default, type: .error, error.localizedDescription)
self?.notifyEvent(eventType: .error, params: [
"errorCode": "ASYNC_SYNTHESIS_FAILED",
"errorMessage": error.localizedDescription
])
}
} }
} }

2
pubspec.yaml

@ -1,7 +1,7 @@
name: voitrans name: voitrans
description: "Voitrans - AI Voice Assistant." description: "Voitrans - AI Voice Assistant."
publish_to: "none" publish_to: "none"
version: 1.0.14+37 version: 1.0.15+38
environment: environment:
sdk: ">=3.3.0 <4.0.0" sdk: ">=3.3.0 <4.0.0"

Loading…
Cancel
Save