title: IOS SWIFT - record audio while playing video and only record from microphone
body: <p>I have an app that records the user's audio session while they are playing a video simultaneously, the issue is the playback from the video comes out of the speaker.</p>
  <p>Ideally, I would like to cancel out the audio coming from the speaker and only record the user's voice through the microphone.</p>
  <p>Example App: <strong>FaceTime</strong>
  Facetime cancels out the music you are playing when you are on a FaceTime call and only records the user's voice (<a href="https://www.quora.com/How-does-an-iPhone-cancel-out-the-music-you-are-playing-when-you-FaceTime-someone" rel="nofollow noreferrer">https://www.quora.com/How-does-an-iPhone-cancel-out-the-music-you-are-playing-when-you-FaceTime-someone</a>)</p>
  <p>Below is my code to add the audio session to record the user:</p>
  <pre><code>let audioInputDevice = AVCaptureDevice.default(
      .builtInMicrophone, 
      for: .audio, 
      position: .unspecified
  )
  .default(for: AVMediaType.audio)!
  let session = self.previewLiveCamera.cameraLayer!.session!
          
  //TODO: fix Audio Input, do not mix with other video audio
  do {
      audioInput = try AVCaptureDeviceInput(device: audioInputDevice!)
      session.addInput(audioInput!)
  } catch {
      print(&quot;failed to add audio input&quot;)
  }
          
  for output in session.outputs {
      let connectionOutput = output as! AVCaptureMovieFileOutput
      self.connectionOutput = connectionOutput
      print(&quot;Started reacording to \(fileURL!)&quot;)
      self.connectionOutput.startRecording(to: fileURL!, recordingDelegate: self)        
  }
  </code></pre>
  <p>Furthermore, im setting the videplayer for the video the user views simultaneously:</p>
  <pre><code>let postPlayer = AVPlayer(url: postVideoUrl!)
  cell.playerLayer.player = postPlayer
  </code></pre>