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