title: How to add audio input to AVCaptureSession only when recording without interrupting video preview?
body: <p>I'm struggling with an issue: I have AVCaptureSession with preview layer and I also want to provide haptic feedbacks when user taps on buttons. If I add audioInput to my AVCaptureSession then I'm not able to produce haptic feedback at all. I tried to to add audioInput right before starting record and to remove immediately after stopping but modifying capture session configuration (which I did in serial queue) leads to video preview hiccups (it's disrupted for a fraction of a second). And I still have no idea how Snapchat and Instagram do this trick. One of my guesses was that they somehow configure AVAudioSession but I couldn't figure it out.</p>
  
  <p>My capture session initialization is pretty general so I won't paste it (notable here is that I have <code>captureSession.automaticallyConfiguresApplicationAudioSession = false</code> line and shared AVAudioSession is modified like so <code>AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVideoRecording, options: [.mixWithOthers])</code>), but I'll post my attempt to toggle audio input:</p>
  
  <pre><code>func addAudioInput() {
      self.sessionQueue.async { [unowned self] in
          self.captureSession.beginConfiguration()
          let microphone = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
          if let audioInput = try? AVCaptureDeviceInput(device: microphone), self.captureSession.canAddInput(audioInput) {
              self.captureSession.addInput(audioInput)
          }
          self.captureSession.commitConfiguration()
      }
  }
  
  func removeAudioInput() {
      self.sessionQueue.async { [unowned self] in
          if let audioInput = self.captureSession.inputs.first(where: { ($0 as? AVCaptureDeviceInput)?.device.deviceType == .builtInMicrophone }) as? AVCaptureDeviceInput {
              self.captureSession.beginConfiguration()
              self.captureSession.removeInput(audioInput)
              self.captureSession.commitConfiguration()
          }
      }
  }
  </code></pre>