AVCaptureSession和蓝牙麦克风

4

我将为您翻译涉及IT技术的内容,您需要将一款视频录制应用的音频输入配置为蓝牙麦克风(如果连接了蓝牙麦克风)。

我有以下代码来配置AVCaptureSession的音频输入:

self.captureSession.usesApplicationAudioSession = YES;
self.captureSession.automaticallyConfiguresApplicationAudioSession = NO;

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];

self.microphone = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
audioInput = [AVCaptureDeviceInput deviceInputWithDevice:self.microphone error:&error];

if ([self.captureSession canAddInput:audioInput])
{
   [self.captureSession addInput:audioInput];
}

问题在于,蓝牙麦克风从未显示为可用的捕获设备(虽然已正确配对)。打印出[AVCaptureDevice devices]的结果是: enter image description here 所以,无论我做什么,音频始终来自iPad内置的麦克风。
1个回答

0

我在使用 SFSpeechRecognizer 时遇到了这个问题。我想要从蓝牙麦克风录制音频并将其转换为文本。首先,我在 AVAudioSession 中切换了输入:

// Activate bluetooth devices for recording
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryRecord, with: [.allowBluetooth])
try? AVAudioSession.sharedInstance().setActive(true)

// Configure a bluetooth device for recording if available
let bluetoothRoutes = [AVAudioSessionPortBluetoothHFP]
let availableInputs = AVAudioSession.sharedInstance().availableInputs
if let bluetoothInput = (availableInputs?.filter{ bluetoothRoutes.contains($0.portType) })?.first {
    try? AVAudioSession.sharedInstance().setPreferredInput(bluetoothInput)
}

但是即使将AVCaptureSession的automaticallyConfiguresApplicationAudioSession属性设置为false,AVCaptureDevice.devices()仅显示了内置麦克风,并没有记录来自蓝牙的任何音频。

最终我用AVAudioRecorder替换了AVCaptureDevice,它确实尊重路由更改。现在我可以录制音频到临时文件中,然后将其传递给SFSpeechRecognizer


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接