AVCaptureSession Pausing Session

With the new camera privacy indicator, there is a new need to pause the session at certain times, ie when the full-screen view is presented over the camera preview.

This was the only solution for me:

Code Block
func pauseSession () {
self.sessionQueue.async {
if self.session.isRunning {
self.session.stopRunning()
}
}
}
func resumeSession () {
self.sessionQueue.async {
if !self.session.isRunning {
self.session.startRunning()
}
}
}


This seems to be an expensive operation that takes time.

The issue I seem to have is if pause and resume are called near each other in time, the whole app freezes for about 10 seconds, till going back to being responsive. This is mostly due to it still hasn't finished the last process (whether to stop or start).

Is there a solution to this?

The native camera app seems to do this fine. If you open it, open the last photo, you can see the green indicator on the top right going off (after becoming orange briefly), meaning the session has paused/stopped. If you swipe down on the photo, the session resumes. If you swipe and let it get canceled, quickly swipe again you can see the session pauses and resumes quickly over and over without any issues.
Post not yet marked as solved Up vote post of sle39lvr Down vote post of sle39lvr
1.7k views