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.
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 // setting .1 second delay
DispatchQueue.main.asyncAfter(deadline: when) { //give slight pause helps media finish and save video correctly
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.