|
|
|
@ -70,8 +70,9 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
print("音频录制组件已初始化,跳过重复初始化") |
|
|
|
return |
|
|
|
} |
|
|
|
preConfigureAudioSession() |
|
|
|
|
|
|
|
print("初始化了") |
|
|
|
// 预配置音频会话 |
|
|
|
audioFormat = getOptimalAudioFormat() |
|
|
|
pushAudioStream = SPXPushAudioInputStream() |
|
|
|
|
|
|
|
@ -84,6 +85,22 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
startWriteThread() |
|
|
|
} |
|
|
|
|
|
|
|
// 【新增】预配置音频会话方法 |
|
|
|
/** |
|
|
|
* 预配置音频会话,在应用启动时调用以减少首次录音延迟 |
|
|
|
* 【优化重点】将耗时的音频会话配置提前到初始化阶段 |
|
|
|
*/ |
|
|
|
private func preConfigureAudioSession() { |
|
|
|
do { |
|
|
|
// 设置音频会话类别和模式 |
|
|
|
runMicrophoneCapture() |
|
|
|
stopMicrophoneCapture() |
|
|
|
print("音频会话预配置完成") |
|
|
|
} catch { |
|
|
|
print("音频会话预配置失败: \(error)") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 获取最佳音频格式 (iOS 通常支持标准采样率) |
|
|
|
public func getOptimalAudioFormat() -> AVAudioFormat? { |
|
|
|
let sampleRate: Double = 16000 // iOS 通常支持 16kHz |
|
|
|
@ -373,7 +390,7 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
|
|
|
|
_isWriting=false |
|
|
|
audioEngine?.pause() |
|
|
|
print("stopMicrophoneCapture") |
|
|
|
print("stopMicrophoneCapture") |
|
|
|
|
|
|
|
} |
|
|
|
public func releaseAudioResources() { |
|
|
|
@ -395,6 +412,7 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
case receiver |
|
|
|
case bluetooth |
|
|
|
} |
|
|
|
|
|
|
|
public func setAudioOutputRoute(_ route: AudioOutputRoute) { |
|
|
|
|
|
|
|
do { |
|
|
|
@ -403,7 +421,7 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
print("当前正在识别") |
|
|
|
try audioEngine?.pause() |
|
|
|
// 先停用以避免冲突 |
|
|
|
try audioSession.setActive(false) |
|
|
|
// try audioSession.setActive(false) |
|
|
|
|
|
|
|
} |
|
|
|
print("setAudioOutputRoutecurrent,route=\(route)") |
|
|
|
|