Browse Source

修复tts奔溃问题,允许混音。9.19

newdev_shunjiawei
fdp 1 year ago
parent
commit
b748eaf116
  1. 27
      local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureTtsHelper.swift
  2. 24
      local_plugins/azure_speech/ios/azure_speech/Sources/tools/MicrophoneCapture.swift
  3. 99
      local_plugins/azure_speech/ios/azure_speech/Sources/tools/SimpleAudioReceiver.swift

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

@ -124,10 +124,33 @@ public class AzureTtsHelper: NSObject, ITtsService {
*/ */
public func startspeak(sessionid sessionid: String)-> Bool { public func startspeak(sessionid sessionid: String)-> Bool {
self.sessionid = sessionid self.sessionid = sessionid
print("startspeak sessionid \(sessionid)")
// 重置当前会话的文本计数 // 重置当前会话的文本计数
self.currentSessionTextCount = 0 self.currentSessionTextCount = 0
self.pendingTextCount = 0 self.pendingTextCount = 0
do {
// 新增:使用 AVAudioSession.sharedInstance 配置为“通话模式 + 扬声器外放”
// - 使用 .playAndRecord 以保证通话上下行链路
// - 使用 .voiceChat 获得通话处理(AEC 等)
// - 使用 .defaultToSpeaker 默认走外放
// - 通过 overrideOutputAudioPort(.speaker) 强制切到扬声器
let audioSession = AVAudioSession.sharedInstance()
//try audioSession.setActive(false)
try audioSession.setCategory(.playAndRecord,
mode: .voiceChat,
options: [.defaultToSpeaker,
.mixWithOthers,
.defaultToSpeaker,
.allowBluetoothA2DP,
.allowBluetooth])
try audioSession.overrideOutputAudioPort(.none)
try audioSession.setActive(true)
} catch {
print("音频会话配置失败: \(error.localizedDescription)")
}
return true return true
} }
@ -187,7 +210,7 @@ public class AzureTtsHelper: NSObject, ITtsService {
totalTextCount += text.count totalTextCount += text.count
currentSessionTextCount += text.count currentSessionTextCount += text.count
print("流式合成文本\(text.count),总计文本数量:\(totalTextCount),当前会话:\(currentSessionTextCount)") //print("流式合成文本\(text.count),总计文本数量:\(totalTextCount),当前会话:\(currentSessionTextCount)")
// 清理文本并添加到缓冲区 // 清理文本并添加到缓冲区
let cleanedText = cleanTextForTTS(text) let cleanedText = cleanTextForTTS(text)

24
local_plugins/azure_speech/ios/azure_speech/Sources/tools/MicrophoneCapture.swift

@ -445,30 +445,6 @@ public class MicrophoneCapture: NSObject {
// 安全地处理音频会话 // 安全地处理音频会话
guard let audioSession = audioSession else { return } guard let audioSession = audioSession else { return }
do {
try audioSession.setActive(false)
if self.hasBluetoothDevices {
// 设置音频会话参数
try audioSession.setCategory(.playback,
mode: .videoChat,
options: [
.allowBluetoothA2DP,
.allowBluetooth
]) // 添加音频优先级控制
} else {
try audioSession.setCategory(.playback,
mode: .videoChat,
options: [
.mixWithOthers,
.defaultToSpeaker])
}
try audioSession.overrideOutputAudioPort(.none)
try audioSession.setActive(true)
} catch {
print("音频会话配置失败: \(error.localizedDescription)")
}
} }
/** /**

99
local_plugins/azure_speech/ios/azure_speech/Sources/tools/SimpleAudioReceiver.swift

@ -193,12 +193,15 @@ public class SimpleAudioReceiver: NSObject {
// 确保正在写入状态 // 确保正在写入状态
guard self._isWriting else { return } guard self._isWriting else { return }
// 设置音频会话参数 // 设置音频会话参数
try audioSession.setCategory(.playAndRecord, try audioSession.setCategory(.playAndRecord,
mode: .videoChat, mode: .voiceChat,
options: [.allowBluetoothA2DP, // 允许蓝牙耳机,不占用hfp链路 options: [.defaultToSpeaker,
.mixWithOthers, .mixWithOthers,
.defaultToSpeaker]) // 添加音频优先级控制 .defaultToSpeaker,
try audioSession.setActive(true) .allowBluetoothA2DP,
.allowBluetooth])
try audioSession.overrideOutputAudioPort(.none)
try audioSession.setActive(true)
try micCapture.startCapture() try micCapture.startCapture()
try micCapture.audioDataHandler = { data in try micCapture.audioDataHandler = { data in
self.writeQueue.put(data) self.writeQueue.put(data)
@ -264,37 +267,7 @@ public class SimpleAudioReceiver: NSObject {
* 强制使用设备内置扬声器或听筒 * 强制使用设备内置扬声器或听筒
*/ */
public func disableBluetoothAudio() { public func disableBluetoothAudio() {
// do {
// print("禁用蓝牙音频功能,切换回正常音频模式")
// // 暂停当前音频引擎
// if _isWriting {
// audioEngine?.pause()
// try audioSession.setActive(false)
// }
// // 设置音频会话为不允许蓝牙
// try audioSession.setCategory(
// .playAndRecord,
// mode: .videoChat,
// options: [.defaultToSpeaker, .mixWithOthers] // 移除 .allowBluetooth
// )
// // 强制使用扬声器
// try audioSession.overrideOutputAudioPort(.speaker)
// // 重新激活音频会话
// if _isWriting {
// try audioSession.setActive(true)
// try audioEngine?.start()
// }
// // 更新当前路由状态
// currentRoute = .speaker
// } catch {
// print("禁用蓝牙音频失败: \(error.localizedDescription)")
// }
} }
/** /**
@ -369,57 +342,7 @@ public func restoreOriginalAudioState() {
public func setAudioOutputRoute(_ route: AudioOutputRoute) { public func setAudioOutputRoute(_ route: AudioOutputRoute) {
// do {
// //
// if self._isWriting {
// print("当前正在识别")
// micCapture.stopCapture()
// // 先停用以避免冲突
// try audioSession.setActive(false)
// }
// print("setAudioOutputRoutecurrent,route=\(route)")
// switch route {
// case .speaker:
// print("setAudioOutputRoutecurrent,speaker")
// // 使用扬声器时必须用videoChat模式
// try audioSession.setCategory(
// .playAndRecord,
// mode: .videoChat,
// options: [.defaultToSpeaker,.allowBluetooth,.mixWithOthers]
// )
// try audioSession.overrideOutputAudioPort(.speaker)
// case .receiver:
// // 听筒模式使用voiceChat节省资源
// try audioSession.setCategory(
// .playAndRecord,
// mode: .voiceChat,
// options: [.allowBluetooth]
// )
// try audioSession.overrideOutputAudioPort(.none)
// case .bluetooth:
// // 完整蓝牙设备支持
// try audioSession.setCategory(
// .playAndRecord,
// mode: .voiceChat,
// options: [.allowBluetooth, .allowBluetoothA2DP]
// )
// try audioSession.overrideOutputAudioPort(.none)
// // 不需要override,系统自动路由
// }
// self.currentRoute = route
// if self._isWriting {
// // 重新激活
// try audioSession.setActive(true, options: [.notifyOthersOnDeactivation])
// try micCapture.startCapture()
// }
// } catch {
// print("路由切换失败: \(error)")
// }
} }

Loading…
Cancel
Save