From 95baa4af72b2ebb4403d369bd6e96e2d62a657a2 Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Sat, 20 Sep 2025 13:22:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BD=95=E9=9F=B3=E5=AE=8C?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E6=92=AD=E6=94=BE=E5=A4=96=E6=94=BE=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82=E4=BF=AE=E5=A4=8D=E5=BD=95=E9=9F=B3=E4=BB=8E?= =?UTF-8?q?=E8=80=B3=E6=9C=BA=E5=BD=95=E9=9F=B3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/tools/MicrophoneCapture.swift | 26 +++++++++++++++++++ .../Sources/tools/SimpleAudioReceiver.swift | 16 +++++------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/local_plugins/azure_speech/ios/azure_speech/Sources/tools/MicrophoneCapture.swift b/local_plugins/azure_speech/ios/azure_speech/Sources/tools/MicrophoneCapture.swift index 1d14da379..2bd895836 100644 --- a/local_plugins/azure_speech/ios/azure_speech/Sources/tools/MicrophoneCapture.swift +++ b/local_plugins/azure_speech/ios/azure_speech/Sources/tools/MicrophoneCapture.swift @@ -444,6 +444,32 @@ public class MicrophoneCapture: NSObject { // 安全地处理音频会话 guard let audioSession = audioSession else { return } + do { + try audioSession.setActive(false) + + if self.hasBluetoothDevices { + print("耳机") + // 设置音频会话参数 + try audioSession.setCategory(.playback, + mode: .videoChat, + options: [ + .allowBluetoothA2DP, + .allowBluetooth + ]) // 添加音频优先级控制 + } else { + print("手机") + try audioSession.setCategory(.playback, + mode: .videoChat, + options: [ + .mixWithOthers, + .defaultToSpeaker]) + } + + try audioSession.overrideOutputAudioPort(.none) + try audioSession.setActive(true) + } catch { + print("音频会话配置失败: \(error.localizedDescription)") + } } 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 b37cd56f4..8d3c4f774 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 @@ -192,15 +192,13 @@ public class SimpleAudioReceiver: NSObject { do { // 确保正在写入状态 guard self._isWriting else { return } - // 设置音频会话参数 - try audioSession.setCategory(.playAndRecord, - mode: .voiceChat, - options: [.defaultToSpeaker, - .mixWithOthers, - .defaultToSpeaker, - .allowBluetoothA2DP, - .allowBluetooth]) - try audioSession.overrideOutputAudioPort(.none) + // 设置音频会话参数 + try audioSession.setCategory(.playAndRecord, + mode: .videoChat, + options: [.allowBluetoothA2DP, // 允许蓝牙耳机,不占用hfp链路 + .mixWithOthers, + .defaultToSpeaker]) // 添加音频优先级控制 + try audioSession.setActive(true) try micCapture.startCapture() try micCapture.audioDataHandler = { data in