Stop recording speaker sound when recording on mic in swift

I am trying to use AVAudioEngine for recording and playing for a voice chat kind of app, but when the speaker plays any audio while recording, the recording take the speaker audio as input. I want to filter that out. Are there any suggestions for the swift code

Stop recording speaker sound when recording on mic in swift
 
 
Q