|
|
@ -198,7 +198,7 @@ public class SimpleAudioReceiver: NSObject { |
|
|
* @throws 音频配置或启动错误 |
|
|
* @throws 音频配置或启动错误 |
|
|
*/ |
|
|
*/ |
|
|
func startCapture(bufferHandler: @escaping (AVAudioPCMBuffer) -> Void) throws { |
|
|
func startCapture(bufferHandler: @escaping (AVAudioPCMBuffer) -> Void) throws { |
|
|
try setupAudioSession() |
|
|
|
|
|
|
|
|
|
|
|
guard let audioEngine = audioEngine else { |
|
|
guard let audioEngine = audioEngine else { |
|
|
throw NSError(domain: "SimpleAudioReceiver", code: -1, userInfo: [NSLocalizedDescriptionKey: "Audio engine not initialized"]) |
|
|
throw NSError(domain: "SimpleAudioReceiver", code: -1, userInfo: [NSLocalizedDescriptionKey: "Audio engine not initialized"]) |
|
|
@ -216,7 +216,7 @@ public class SimpleAudioReceiver: NSObject { |
|
|
|
|
|
|
|
|
// 重置音频引擎(在获取格式之前) |
|
|
// 重置音频引擎(在获取格式之前) |
|
|
audioEngine.reset() |
|
|
audioEngine.reset() |
|
|
|
|
|
try setupAudioSession() |
|
|
// 重新获取输入节点和格式(reset后需要重新获取) |
|
|
// 重新获取输入节点和格式(reset后需要重新获取) |
|
|
let inputNode = audioEngine.inputNode |
|
|
let inputNode = audioEngine.inputNode |
|
|
let inputFormat = inputNode.outputFormat(forBus: 0) |
|
|
let inputFormat = inputNode.outputFormat(forBus: 0) |
|
|
@ -286,15 +286,15 @@ public class SimpleAudioReceiver: NSObject { |
|
|
print("⏹️ 停止现有音频引擎") |
|
|
print("⏹️ 停止现有音频引擎") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// try startCapture { buffer in |
|
|
try startCapture { buffer in |
|
|
// // 处理实时音频数据 |
|
|
// 处理实时音频数据 |
|
|
// let channelData = buffer.floatChannelData?[0] |
|
|
let channelData = buffer.floatChannelData?[0] |
|
|
// let frameCount = buffer.frameLength |
|
|
let frameCount = buffer.frameLength |
|
|
// print("麦克风启动: \(frameCount)") |
|
|
print("麦克风启动: \(frameCount)") |
|
|
// print("🎵 音频数据: 帧数=\(frameCount), 声道数据=\(channelData != nil ? "有效" : "无效")") |
|
|
print("🎵 音频数据: 帧数=\(frameCount), 声道数据=\(channelData != nil ? "有效" : "无效")") |
|
|
// self.writeQueue.put(self.audioBufferToData(buffer)) |
|
|
self.writeQueue.put(self.audioBufferToData(buffer)) |
|
|
// // 分析或传输数据... |
|
|
// 分析或传输数据... |
|
|
// } |
|
|
} |
|
|
try audioEngine?.start() |
|
|
try audioEngine?.start() |
|
|
print("✅ 麦克风捕获配置完成") |
|
|
print("✅ 麦克风捕获配置完成") |
|
|
|
|
|
|
|
|
@ -430,16 +430,18 @@ private func audioBufferToData(_ buffer: AVAudioPCMBuffer) -> Data { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 设置音频会话配置 |
|
|
* 配置音频会话 |
|
|
* 确保音频会话正确配置用于录音 |
|
|
* 设置录音和播放所需的音频会话参数 |
|
|
|
|
|
* @throws 音频会话配置失败时抛出错误 |
|
|
*/ |
|
|
*/ |
|
|
private func setupAudioSession() throws { |
|
|
private func setupAudioSession() throws { |
|
|
try audioSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth]) |
|
|
try audioSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth]) |
|
|
try audioSession.setActive(true) |
|
|
try audioSession.setActive(true) |
|
|
|
|
|
|
|
|
// 设置首选的音频参数 |
|
|
// 设置首选的音频参数 |
|
|
try audioSession.setPreferredSampleRate(48000) |
|
|
try audioSession.setPreferredSampleRate(48000) |
|
|
try audioSession.setPreferredInputNumberOfChannels(1) |
|
|
// 设置输入声道数 |
|
|
|
|
|
try audioSession.setPreferredInputNumberOfChannels(1) |
|
|
|
|
|
|
|
|
print("音频会话配置成功: 采样率=\(audioSession.sampleRate)Hz, 输入声道=\(audioSession.inputNumberOfChannels)") |
|
|
print("音频会话配置成功: 采样率=\(audioSession.sampleRate)Hz, 输入声道=\(audioSession.inputNumberOfChannels)") |
|
|
} |
|
|
} |
|
|
|