|
|
|
@ -264,12 +264,25 @@ import UIKit |
|
|
|
if !isConfigured { |
|
|
|
NSLog("[AudioSessionManager] 警告: 尝试激活未配置的音频会话,应用默认配置") |
|
|
|
// 未配置,应用默认配置 |
|
|
|
try configureSession( |
|
|
|
category: .playAndRecord, |
|
|
|
mode: .spokenAudio, |
|
|
|
options: [.allowBluetooth, .allowBluetoothA2DP, .duckOthers, .defaultToSpeaker], |
|
|
|
force: true |
|
|
|
// try configureSession( |
|
|
|
// category: .playAndRecord, |
|
|
|
// mode: .spokenAudio, |
|
|
|
// options: [.allowBluetooth, .allowBluetoothA2DP, .duckOthers, .defaultToSpeaker], |
|
|
|
// force: true |
|
|
|
// ) |
|
|
|
|
|
|
|
try configureSession( |
|
|
|
category: .playAndRecord, // 允许同时录音和播放 |
|
|
|
mode: .voiceChat, // 使用voiceChat模式获得最佳回音消除效果 |
|
|
|
options: [ |
|
|
|
.allowBluetooth, // 允许蓝牙设备 |
|
|
|
.defaultToSpeaker, // 默认使用扬声器 |
|
|
|
.mixWithOthers // 允许与其他应用混音 |
|
|
|
], |
|
|
|
force: false // 是否强制重新配置 |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
} else if !isActive { |
|
|
|
NSLog("[AudioSessionManager] 激活已配置的音频会话") |
|
|
|
try audioSession.setActive(true, options: .notifyOthersOnDeactivation) |
|
|
|
@ -289,12 +302,24 @@ import UIKit |
|
|
|
/// 重新配置当前会话 |
|
|
|
@objc func forceReconfigure() throws { |
|
|
|
NSLog("[AudioSessionManager] 强制重新配置音频会话") |
|
|
|
try configureSession( |
|
|
|
category: currentCategory ?? .playAndRecord, |
|
|
|
mode: currentMode ?? .default, |
|
|
|
options: currentOptions ?? [.allowBluetooth, .allowBluetoothA2DP, .defaultToSpeaker], |
|
|
|
force: true |
|
|
|
) |
|
|
|
// try configureSession( |
|
|
|
// category: currentCategory ?? .playAndRecord, |
|
|
|
// mode: currentMode ?? .default, |
|
|
|
// options: currentOptions ?? [.allowBluetooth, .allowBluetoothA2DP, .defaultToSpeaker], |
|
|
|
// force: true |
|
|
|
// ) |
|
|
|
|
|
|
|
try configureSession( |
|
|
|
category: .playAndRecord, // 允许同时录音和播放 |
|
|
|
mode: .voiceChat, // 使用voiceChat模式获得最佳回音消除效果 |
|
|
|
options: [ |
|
|
|
.allowBluetooth, // 允许蓝牙设备 |
|
|
|
.defaultToSpeaker, // 默认使用扬声器 |
|
|
|
.mixWithOthers // 允许与其他应用混音 |
|
|
|
], |
|
|
|
force: false // 是否强制重新配置 |
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// MARK: - 状态检查 |
|
|
|
@ -420,60 +445,60 @@ import UIKit |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// 为Azure语音识别专门配置的音频会话 |
|
|
|
/// 此函数优化Azure语音识别的音频处理配置,提供更强的回音消除能力 |
|
|
|
/// - Parameters: |
|
|
|
/// - force: 是否强制重新配置 |
|
|
|
/// - configureAdditionalSettings: 配置完成后进行额外的音频设置 |
|
|
|
/// - Returns: 配置是否成功 |
|
|
|
@objc func configureForAzureSpeechRecognition(force: Bool = false, configureAdditionalSettings: Bool = true) -> Bool { |
|
|
|
NSLog("[AudioSessionManager] 配置Azure语音识别的音频会话") |
|
|
|
do { |
|
|
|
// 配置音频会话以优化语音识别 |
|
|
|
try configureSession( |
|
|
|
category: .playAndRecord, // 允许同时录音和播放 |
|
|
|
mode: .voiceChat, // 使用voiceChat模式获得最佳回音消除效果 |
|
|
|
options: [ |
|
|
|
.allowBluetooth, // 允许蓝牙设备 |
|
|
|
.defaultToSpeaker, // 默认使用扬声器 |
|
|
|
.mixWithOthers // 允许与其他应用混音 |
|
|
|
], |
|
|
|
force: force // 是否强制重新配置 |
|
|
|
) |
|
|
|
// /// 为Azure语音识别专门配置的音频会话 |
|
|
|
// /// 此函数优化Azure语音识别的音频处理配置,提供更强的回音消除能力 |
|
|
|
// /// - Parameters: |
|
|
|
// /// - force: 是否强制重新配置 |
|
|
|
// /// - configureAdditionalSettings: 配置完成后进行额外的音频设置 |
|
|
|
// /// - Returns: 配置是否成功 |
|
|
|
// @objc func configureForAzureSpeechRecognition(force: Bool = false, configureAdditionalSettings: Bool = true) -> Bool { |
|
|
|
// NSLog("[AudioSessionManager] 配置Azure ASR语音识别的音频会话") |
|
|
|
// do { |
|
|
|
// // 配置音频会话以优化语音识别 |
|
|
|
// try configureSession( |
|
|
|
// category: .playAndRecord, // 允许同时录音和播放 |
|
|
|
// mode: .voiceChat, // 使用voiceChat模式获得最佳回音消除效果 |
|
|
|
// options: [ |
|
|
|
// .allowBluetooth, // 允许蓝牙设备 |
|
|
|
// .defaultToSpeaker, // 默认使用扬声器 |
|
|
|
// .mixWithOthers // 允许与其他应用混音 |
|
|
|
// ], |
|
|
|
// force: force // 是否强制重新配置 |
|
|
|
// ) |
|
|
|
|
|
|
|
// 额外的优化配置 |
|
|
|
if configureAdditionalSettings { |
|
|
|
// 获取当前是否使用耳机 |
|
|
|
let currentRoute = audioSession.currentRoute |
|
|
|
let hasHeadphones = currentRoute.outputs.contains { |
|
|
|
$0.portType == .headphones || $0.portType == .bluetoothA2DP || $0.portType == .bluetoothHFP |
|
|
|
} |
|
|
|
// // 额外的优化配置 |
|
|
|
// if configureAdditionalSettings { |
|
|
|
// // 获取当前是否使用耳机 |
|
|
|
// let currentRoute = audioSession.currentRoute |
|
|
|
// let hasHeadphones = currentRoute.outputs.contains { |
|
|
|
// $0.portType == .headphones || $0.portType == .bluetoothA2DP || $0.portType == .bluetoothHFP |
|
|
|
// } |
|
|
|
|
|
|
|
// 设置采样率为16kHz(Azure Speech API推荐) |
|
|
|
try audioSession.setPreferredSampleRate(16000.0) |
|
|
|
// // 设置采样率为16kHz(Azure Speech API推荐) |
|
|
|
// try audioSession.setPreferredSampleRate(16000.0) |
|
|
|
|
|
|
|
// 设置较小的缓冲区大小以减少延迟 |
|
|
|
try audioSession.setPreferredIOBufferDuration(0.01) |
|
|
|
// // 设置较小的缓冲区大小以减少延迟 |
|
|
|
// try audioSession.setPreferredIOBufferDuration(0.01) |
|
|
|
|
|
|
|
// 根据是否有耳机连接调整输入增益 |
|
|
|
if !hasHeadphones { |
|
|
|
// 无耳机时降低输入增益以减少回音 |
|
|
|
try audioSession.setInputGain(0.8) |
|
|
|
NSLog("[AudioSessionManager] 启用扬声器回音消除优化") |
|
|
|
} else { |
|
|
|
// 使用耳机时可以使用较高增益 |
|
|
|
try audioSession.setInputGain(1.0) |
|
|
|
NSLog("[AudioSessionManager] 检测到耳机连接,应用耳机模式") |
|
|
|
} |
|
|
|
} |
|
|
|
// // 根据是否有耳机连接调整输入增益 |
|
|
|
// if !hasHeadphones { |
|
|
|
// // 无耳机时降低输入增益以减少回音 |
|
|
|
// try audioSession.setInputGain(0.8) |
|
|
|
// NSLog("[AudioSessionManager] 启用扬声器回音消除优化") |
|
|
|
// } else { |
|
|
|
// // 使用耳机时可以使用较高增益 |
|
|
|
// try audioSession.setInputGain(1.0) |
|
|
|
// NSLog("[AudioSessionManager] 检测到耳机连接,应用耳机模式") |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
NSLog("[AudioSessionManager] 已成功配置Azure语音识别的音频会话") |
|
|
|
return true |
|
|
|
} catch { |
|
|
|
NSLog("[AudioSessionManager] 配置Azure语音识别的音频会话失败: \(error.localizedDescription)") |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
// NSLog("[AudioSessionManager] 已成功配置Azure语音识别的音频会话") |
|
|
|
// return true |
|
|
|
// } catch { |
|
|
|
// NSLog("[AudioSessionManager] 配置Azure语音识别的音频会话失败: \(error.localizedDescription)") |
|
|
|
// return false |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
@objc func configureForVoiceInteraction(force: Bool = false, configureAdditionalSettings: Bool = true) -> Bool { |
|
|
|
|