【问题标题】:No bluetooth mic input with AVAudioSession.setCateogryAVAudioSession.setCateogry 没有蓝牙麦克风输入
【发布时间】:2021-05-03 22:33:27
【问题描述】:

我正在尝试使用录音机来捕捉来自 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

【问题讨论】:

  • 有什么帮助吗?
  • 您好!我知道这是不被接受的。但是很长一段时间我无法解决这个问题。你可以帮帮我吗?我会很感激。我在我所在的地区找不到 AirPods Pro。你能打开我的小xcode项目并测试一下吗? stackoverflow.com/questions/69851479/…

标签: swift xcode avfoundation


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 您能谈谈这将如何启用 AirPod 的麦克风吗?注意prepareToRecordrecord 自动调用。永远不应该有理由在record 之前立即调用它。如果您知道稍后要录制并希望录制更快地开始,那么提前调用才有用,然后现在进行初始化。
    • 所以我的代码中确实已经有了@Rob Napier 建议的设置,但我认为 SwiftUI 只是有问题,直到最新更新才出现,现在事情似乎按预期工作。感谢您的建议!
    猜你喜欢
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多