|
|
@ -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)") |
|
|
|
|
|
// } |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|