AVAudioSession.setCateogry 没有蓝牙麦克风输入

时间:2021-05-03 22:33:27

标签: swift xcode avfoundation

我正在尝试使用录音机来捕获来自 AirPods Pro 的输入。以下函数执行音频捕获,它适用于内置麦克风,但不适用于 AirPods。该问题在模拟器和真实设备上仍然存在。有人可以指出我在这里缺少什么以启用蓝牙捕获吗?谢谢!

func startRecording() {
        let recordingSession = AVAudioSession.sharedInstance()
        
        // Then we start recording session with
        do {
            try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth, .defaultToSpeaker, .allowBluetoothA2DP])
            try recordingSession.setActive(true)
        } catch {
            print("Failed to set up recording session")
            
        }
        
        // Then we say where to save it
        let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        
        // file is named after the date and time of the recording in the .m4a format.
        let audioFilename = documentPath.appendingPathComponent("\(Date().toString( dateFormat: "dd-MM-YY_'at'_HH:mm:ss")).m4a")
        
        let settings = [
                    AVFormatIDKey: Int(kAudioFormatLinearPCM),
                    AVSampleRateKey: 48000,
                    AVNumberOfChannelsKey: 1,
                    AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
                ]
        
        do {
            audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
            audioRecorder.record()
            recording = true
        } catch {
            print("Could not start recording")
        }
    } //end func startRecording

2 个答案:

答案 0 :(得分:0)

试试这个:

    audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
    audioRecorder.prepareToRecord()
    audioRecorder.record()

答案 1 :(得分:0)

您的问题在于您的选择:[.allowBluetooth, .defaultToSpeaker, .allowBluetoothA2DP]。 A2DP 不支持录音,因此当您将设备置于 A2DP 模式时,其麦克风将被禁用。您需要删除 .allowBluetoothA2DP 选项

请注意,当您执行此操作时,它会切换到 HFP 模式(这就是 .allowBluetooth 的实际含义)。这将显着降低音频输出。对于录音机来说可能没问题,但如果您在播放高质量音频的同时尝试从 AirPod 的麦克风录音,则不支持。

相关问题