【问题标题】:AVCaptureSession and bluetooth micAVCaptureSession 和蓝牙麦克风
【发布时间】:2015-07-13 08:08:30
【问题描述】:

我正在开发一个视频录制应用程序,并且需要能够使用蓝牙麦克风作为音频输入(如果已连接)。

我有以下代码来配置 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] 结果:

所以,无论我做什么,音频总是来自 iPad 的内置麦克风。

【问题讨论】:

    标签: avcapturesession ios-bluetooth


    【解决方案1】:

    我也在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() 也只向我显示了内置麦克风,并没有从蓝牙录制任何音频。

    我最终将AVCaptureDevice 替换为AVAudioRecorder,这确实尊重路线变化。现在我可以将音频录制到一个临时文件中,然后传递给SFSpeechRecognizer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多