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