Weird AVMutableComposition behavior when headphones are plugged in

I have an app that streams mov files. One of the features in my app is recording audio over these streamed files. In order to accomplish that I download the streamed file in the background and then commence audio recording to capture user audio. Once the user is done I take the recorded audio and merge it with the mov file i previously downloaded in the background.

All of this is working well except for when you plug in headphones. The experience is the same but when you go to playback the recording only the audio was captured. The mov file never makes it into the final asset not sure why.

Something similar happens when the iOS device has it's audio output set to a Bluetooth device. The final recording captures the user recorded audio and the video in the mov file but not the audio in the mov file. What gives?

addMutableTrackWithMediaType adds a track of the specified media type. By specifying AVMediaTypeVideo when i add the video track it ONLY adds the video track. Therefore, when the headphones are not plugged in the audio recorder picks up the audio from the mov that is playing, however, when the headphones are plugged in the mic cannot pick up the audio playing through the earbuds.

The solution is to add both the audio and video track of the cached mov file and deal with the lag/synchronization of all tracks. The audio of the cached mov should also be added conditionally if the headphones are plugged or if the audio output on the device is set to something like a bluetooth speaker in which case the audio recorder will not be able to pick up that audio as cleanly.

Open to other solutions suggestions. But this approach is working for me for now.