Browse Source

tts还原

weicu
fdp 1 year ago
parent
commit
f8ec388faa
  1. 100
      local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureTtsHelper.swift

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

@ -264,74 +264,44 @@ public class AzureTtsHelper: NSObject, ITtsService {
pendingTasks.removeAll() pendingTasks.removeAll()
taskLock.unlock() taskLock.unlock()
// 使用异步停止方法并等待完成 do {
let stopGroup = DispatchGroup() print("liwei-------------停止语音合成播放")
stopGroup.enter() try self.synthesizer?.stopSpeaking()
self.notifyEvent(eventType: .synthesisCanceled)
synthesisQueue.async { [weak self] in
guard let self = self else {
stopGroup.leave()
return
}
if !micCapture.isCapturing{
// 停止时设置音频会话为非活跃状态
// try AVAudioSession.sharedInstance().setActive(false)
}
print("音频会话已设置为非活跃状态")
} catch {
os_log("停止语音合成失败: %{public}@", log: self.log, type: .error, error.localizedDescription)
self.notifyEvent(eventType: .error, params: [
"errorCode": "STOP_FAILED",
"errorMessage": error.localizedDescription
])
}
synthesisQueue.async {
do { do {
print("liwei-------------停止语音合成播放")
// 使用同步停止方法
if let synthesizer = self.synthesizer {
try synthesizer.stopSpeaking()
}
self.notifyEvent(eventType: .synthesisCanceled)
if !self.micCapture.isCapturing {
// 停止时设置音频会话为非活跃状态
// try AVAudioSession.sharedInstance().setActive(false)
}
print("音频会话已设置为非活跃状态")
// 等待所有合成操作完成后再重新创建
self.synthesisGroup.wait()
// 安全地销毁和重新创建合成器
self.synthesizer = nil self.synthesizer = nil
try self.recreateSynthesizer() try self.recreateSynthesizer()
}catch {
} catch { os_log("重置语音合成失败: %{public}@", log: self.log, type: .error, error.localizedDescription)
os_log("停止语音合成失败: %{public}@", log: self.log, type: .error, error.localizedDescription)
self.notifyEvent(eventType: .error, params: [
"errorCode": "STOP_FAILED",
"errorMessage": error.localizedDescription
])
} }
stopGroup.leave()
} }
// 等待停止操作完成
stopGroup.wait()
return true return true
} }
/** /**
* 释放资源 * 释放资源
*/ */
public func dispose() { public func dispose() {
// 先停止所有操作
_ = stop() _ = stop()
// 等待所有异步操作完成
synthesisGroup.wait() synthesisGroup.wait()
synthesisQueue.sync { synthesisQueue.sync {
// 确保合成器完全停止后再销毁
if let synthesizer = self.synthesizer {
do {
try synthesizer.stopSpeaking()
} catch {
os_log("停止合成器失败: %{public}@", log: self.log, type: .error, error.localizedDescription)
}
}
synthesizer = nil synthesizer = nil
speechConfig = nil speechConfig = nil
customAudioOutputStream = nil customAudioOutputStream = nil
@ -471,45 +441,29 @@ public class AzureTtsHelper: NSObject, ITtsService {
/** /**
* 执行语音合成 * 执行语音合成
*/ */
private func performSynthesis(sessionid: String, text: String) { private func performSynthesis(sessionid:String,text: String) {
if (self.sessionid != sessionid) { if (self.sessionid != sessionid) {
return return
} }
// 进入合成组
synthesisGroup.enter()
speaking = true speaking = true
// lastSpokenText = text
// 生成SSML // 生成SSML
let ssml = generateOptimizedSsml(text) let ssml = generateOptimizedSsml(text)
do { do {
if (self.sessionid != sessionid) { if (self.sessionid != sessionid) {
synthesisGroup.leave()
return return
} }
os_log("开始合成: %{public}@", log: log, type: .debug, ssml) os_log("开始合成: %{public}@", log: log, type: .debug, ssml)
let result = try synthesizer?.startSpeakingSsml(ssml)
// 使用同步合成方法 if let result = result {
if let synthesizer = self.synthesizer { os_log("合成完成,结果: %{public}@", log: log, type: .debug, String(describing: result.reason))
do {
try synthesizer.startSpeakingSsml(ssml)
os_log("合成开始成功", log: self.log, type: .debug)
} catch {
self.speaking = false
os_log("合成失败: %{public}@", log: self.log, type: .error, error.localizedDescription)
self.notifyEvent(eventType: .error, params: [
"errorCode": "SYNTHESIS_FAILED",
"errorMessage": error.localizedDescription
])
}
} }
} catch { } catch {
speaking = false speaking = false
// 减少待处理文本计数
pendingTextCount = max(0, pendingTextCount - text.count) pendingTextCount = max(0, pendingTextCount - text.count)
synthesisGroup.leave()
os_log("语音合成失败: %{public}@", log: log, type: .error, error.localizedDescription) os_log("语音合成失败: %{public}@", log: log, type: .error, error.localizedDescription)
notifyEvent(eventType: .error, params: [ notifyEvent(eventType: .error, params: [
"errorCode": "SYNTHESIS_FAILED", "errorCode": "SYNTHESIS_FAILED",

Loading…
Cancel
Save