Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding AVCaptureDeviceInput to CaptureSession resets/refocuses video

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)?

like image 838
Tanel Teemusk Avatar asked Sep 10 '25 00:09

Tanel Teemusk


1 Answers

I've noticed AVCaptureSession addInput resets silence set by AVAudioSession AVAudioSessionCategory(Solo)Ambient category

 Category                              Silenced   Interrupts
 AVAudioSessionCategoryAmbient         Yes        No
 AVAudioSessionCategorySoloAmbient     Yes        Yes
 AVAudioSessionCategoryMultiRoute      No         Yes
 AVAudioSessionCategoryPlayAndRecord   No         Yes
 AVAudioSessionCategoryPlayback        No         Yes
 AVAudioSessionCategoryRecord          No         Yes

https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html

When the user moves the Silent switch (or Ring/Silent switch on iPhone) to the “silent” position, your audio is silenced.

AVAudioSessionCategoryAmbient—Playback only. Plays sounds that add polish or interest but are not essential to the app’s use. Using this category, your audio is silenced by the Ring/Silent switch and when the screen locks.

AVAudioSessionCategorySoloAmbient—(Default) Playback only. Silences audio when the user switches the Ring/Silent switch to the “silent” position and when the screen locks. This category differs from the AVAudioSessionCategoryAmbient category only in that it interrupts other audio.

like image 126
Isaak Osipovich Dunayevsky Avatar answered Sep 12 '25 13:09

Isaak Osipovich Dunayevsky