A video recording app. I want it to work without stopping/pausing background music (when user listens to Apple Music for instance). This I can do nicely with setting category to mixWithOthers
on AVAudioSession
singleton.
After setting the category I also need to add AVCaptureDeviceInput
to AVCaptureSession
(so audio will get recorded). This results a glitch/hiccup to background audio and also video resets/refocuses.
I have investigated and it seems background audio glitch is something that can't be avoided, but video should not reset itself when input is added. The result of video resetting is that the first frame of the recorded video is either dark/black or it starts with out of focus frame and then focuses.
Also checked Snapchat ios app and they also have audio glitch when starting recording, but video starts to record smoothly. What am I doing wrong.
My code:
//Setting audio session to mixWithOthers upon startup
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord,
with: [.mixWithOthers])
if session.mode != AVAudioSessionModeVideoRecording {
try session.setMode(AVAudioSessionModeVideoRecording)
}
} catch let error {
print("avsession category error: \(error)")
}
And then:
//Just before recording starts will add audio input
let audioDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
do
{
let deviceInput = try AVCaptureDeviceInput(device: audioDevice) as AVCaptureDeviceInput
if captureSession.canAddInput(deviceInput) {
captureSession.addInput(deviceInput)
}
else {
print("Could not add audio device input to the session")
}
}
catch let error as NSError {
print(error.localizedDescription)
return
}
Would it be possible to do this without any glitches at all? If not then how could I make it at least like Snapchat (no video reset upon addInput call)?