Recording from Built-In Mic when Playing through Bluetooth in iOS

前端 未结 2 955
情歌与酒
情歌与酒 2020-12-18 04:59

Is it possible to receive audio input from iPhone\'s built-in microphone, and play that audio through a Bluetooth headset, at the same time?

My goal is to always use

相关标签:
2条回答
  • 2020-12-18 05:09

    You can choose a specific microphone while playing audio through a bluetooth audio device.

      // set audio session category to .playAndRecord. use do-catch if you need error-handling
      try? AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetoothA2DP, .allowBluetooth])
    
      // check if currentRoute is set to a bluetooth audio device
      let btOutputTypes: [AVAudioSession.Port] = [.bluetoothHFP, .bluetoothA2DP, .bluetoothLE]
      let btOutputs = AVAudioSession.sharedInstance().currentRoute.outputs.filter { btOutputTypes.contains($0.portType) }
    
      // if so, set preferred audio input to built-in mic
      if !btOutputs.isEmpty {
         let builtInMicInput = AVAudioSession.sharedInstance().availableInputs?.filter { $0.portType == .builtInMic }.first
         try? AVAudioSession.sharedInstance().setPreferredInput(builtInMicInput)
      } else {
         // set default input
         try? AVAudioSession.sharedInstance().setPreferredInput(nil)
      }
    
      try? AVAudioSession.sharedInstance().setActive(true)
    

    or you can follow detailed instruction from here https://developer.apple.com/library/archive/qa/qa1799/_index.html

    0 讨论(0)
  • 2020-12-18 05:25

    It is not possible to receive audio input from iPhone's built-in microphone, and play that audio through a Bluetooth headset, at the same time

    1. There is a prevention that you can use like a walkie-talkie .
    2. you have to create Two sessions . Means while recording audio you have to enable audio session with recordandplay.
    3. while Playing you have to set the option to audiosession (allowingBluetooth) .
    4. While Recording You have to set recordandplay.
    0 讨论(0)
提交回复
热议问题