|
|
|
@ -365,7 +365,82 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// MARK: - 协议定义 |
|
|
|
/** |
|
|
|
* 禁用蓝牙音频功能,切换回正常音频模式 |
|
|
|
* 强制使用设备内置扬声器或听筒 |
|
|
|
*/ |
|
|
|
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)") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 恢复原始音频设备状态(重新启用蓝牙) |
|
|
|
* 允许系统自动选择最佳音频设备 |
|
|
|
*/ |
|
|
|
public func restoreOriginalAudioState() { |
|
|
|
do { |
|
|
|
print("恢复原始音频设备状态(重新启用蓝牙)") |
|
|
|
|
|
|
|
// 暂停当前音频引擎 |
|
|
|
if _isWriting { |
|
|
|
audioEngine?.pause() |
|
|
|
try audioSession.setActive(false) |
|
|
|
} |
|
|
|
|
|
|
|
// 恢复完整的蓝牙音频支持 |
|
|
|
try audioSession.setCategory( |
|
|
|
.playAndRecord, |
|
|
|
mode: .videoChat, |
|
|
|
options: [.defaultToSpeaker, .allowBluetooth, .mixWithOthers] |
|
|
|
) |
|
|
|
|
|
|
|
// 取消音频端口覆盖,让系统自动选择 |
|
|
|
try audioSession.overrideOutputAudioPort(.none) |
|
|
|
|
|
|
|
// 重新激活音频会话 |
|
|
|
if _isWriting { |
|
|
|
try audioSession.setActive(true) |
|
|
|
try audioEngine?.start() |
|
|
|
} |
|
|
|
|
|
|
|
// 重置路由状态,让系统自动选择 |
|
|
|
currentRoute = nil |
|
|
|
|
|
|
|
} catch { |
|
|
|
print("恢复原始音频状态失败: \(error.localizedDescription)") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public func stopMicrophoneCapture() { |
|
|
|
@ -568,5 +643,5 @@ private class LinkedBlockingQueue<T> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// MARK: - 协议定义 |
|
|
|
|
|
|
|
|
|
|
|
|