Handling AVCapturesession after returning from background

I am using VideoRecorder using AVCaptureSession. I run AVCaptureSession in viewWillAppear and tear it to viewWillDisappear on the recommendation of this question AVCaptureSession error when returning from the background.Now when the video is being recorded and the application goes to the background, I want to stop recording and pause the capture session. But every time the application comes to the fore at this moment, I get one of the following

  • Session capture is not paused, but the recording and preliminary level are retained.
  • Capture a session provides a preliminary layer with a black screen at this point, the application may or may not crash.

Any suggestions on handling AVCaptureSession at the moment. I would just like to show the last frame recorded on the preliminary layer after stopping recording.

+1
source share
2 answers

I faced a similar situation, and from my own experience I found that viewWillDisappear: does not receive the call. I really don't know why, but I solved this by signing up for notifications when the application is inactive. Here is an example:

In the WillAppear view:

// Detect this for ending recording
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appInactive:) name:UIApplicationWillResignActiveNotification object:[UIApplication sharedApplication]];

And the corresponding callback method:

- (void)appInactive:(NSNotification *)notification {
NSLog(@"App going inactive, stopping recording...");
taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{
    [[UIApplication sharedApplication] endBackgroundTask:taskId];
    taskId = UIBackgroundTaskInvalid;
}];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    question.prepTimeRemaining = [prepEndTime timeIntervalSinceNow];

    // Stop camera stuff
    if (recording)
        [self stopRecording]; // Method to handle shutting down the session, any other cleanup, etc.

    // End task
    [[UIApplication sharedApplication] endBackgroundTask:taskId];
    taskId = UIBackgroundTaskInvalid;
});
}

In the WillDisappear view:

[[NSNotificationCenter defaultCenter] removeObserver:self];

I immediately go on to the next view when I discover this, so I'm not sure if it leaves at the preview level, but I suspect that it will do what you want. Hope this helps!

+1
source

, . , , ViewWillAppear ViewWillDisappear . . :

//application became active
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(applicationEnteredForeground:)
name:UIApplicationWillEnterForegroundNotification
                                           object:nil];

//application went into background
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationBecameActive:)
name:UIApplicationDidBecomeActiveNotification
                                           object:nil];

, , stackoverflow, avcapturesession, .

+3

All Articles