|
|
|
@ -70,7 +70,9 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
print("音频录制组件已初始化,跳过重复初始化") |
|
|
|
return |
|
|
|
} |
|
|
|
preConfigureAudioSession() |
|
|
|
if audioSession.recordPermission == .granted { //判断权限成功 预热麦克风 |
|
|
|
preConfigureAudioSession() |
|
|
|
} |
|
|
|
|
|
|
|
// 预配置音频会话 |
|
|
|
audioFormat = getOptimalAudioFormat() |
|
|
|
@ -229,7 +231,7 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
// 【新增】首先配置音频会话 |
|
|
|
try audioSession.setCategory( |
|
|
|
.playAndRecord, |
|
|
|
mode: .videoChat, |
|
|
|
mode: .default, |
|
|
|
options: [ |
|
|
|
.defaultToSpeaker, |
|
|
|
.allowBluetooth, |
|
|
|
@ -239,13 +241,13 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
) |
|
|
|
|
|
|
|
// 【新增】请求麦克风权限(如果尚未授权) |
|
|
|
// if audioSession.recordPermission != .granted { |
|
|
|
// audioSession.requestRecordPermission { granted in |
|
|
|
// if !granted { |
|
|
|
// print("麦克风权限被拒绝") |
|
|
|
// } |
|
|
|
// } |
|
|
|
// } |
|
|
|
if audioSession.recordPermission != .granted { |
|
|
|
audioSession.requestRecordPermission { granted in |
|
|
|
if !granted { |
|
|
|
print("麦克风权限被拒绝") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 【新增】激活音频会话(在配置音频引擎之前) |
|
|
|
try audioSession.setActive(true) |
|
|
|
@ -272,9 +274,9 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
let hardwareFormat = inputNode.inputFormat(forBus: 0) |
|
|
|
|
|
|
|
// iOS 13+ 启用语音处理 |
|
|
|
if #available(iOS 13.0, *) { |
|
|
|
try inputNode.setVoiceProcessingEnabled(true) |
|
|
|
} |
|
|
|
// if #available(iOS 13.0, *) { |
|
|
|
// try inputNode.setVoiceProcessingEnabled(true) |
|
|
|
// } |
|
|
|
|
|
|
|
// 检查目标音频格式和转换器是否可用 |
|
|
|
guard let targetFormat = audioFormat, // 外部定义的期望音频格式 |
|
|
|
|