How to stop and save a video recording when the app enters the background?

My app records video and I wanted to stop the video and save it when the app enters background. I found that to run anything during the time the app enters the background you use appdelegate's applicationDidEnterBackground, but it doesn't save the video it only stops the recording.


var vc = ViewController()
if(vc.recording){
            print("break")
            vc.recordButton.setBackgroundImage( imageLiteral(resourceName: "start recording"), for: UIControlState.normal)
         
            vc.recording = false
         
            self.vc.videoFileOutput.stopRecording()
            let when = DispatchTime.now() + 0.1 /
            DispatchQueue.main.asyncAfter(deadline: when) { /
                if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum((self.vc.filePath?.relativePath)!) {
                    UISaveVideoAtPathToSavedPhotosAlbum((self.vc.filePath?.relativePath)!, nil, nil, nil)
                }else{
                    print("error could not save")
                }
            }
            vc.cameraSession.beginConfiguration()
            vc.cameraSession.removeOutput(vc.videoFileOutput)
            vc.cameraSession.commitConfiguration()
         
        }
    }

This is my code and I know that the only line that runs is the self.vc.videoFileOutput.stopRecording(). I do not know why this is the only line that runs, but the main purpose is to run the UISaveVideoAtPathtoSavedPhotosAlbum line. I have tried running this in the applicationWillResign, but it still only stops the recording. This is my first app, and I do not know a lot about the app delegate class. I have been trying to solve this a while to no avail, so any help would be appreciated. Thank you.