AVCapturesession handling after returning from background

喜夏-厌秋 提交于 2019-12-07 14:13:38

问题


I am implementing a VideoRecorder using the AVCaptureSession. I am starting AVCaptureSession at viewWillAppear and tearing it down at viewWillDisappear on recommendation of this question AVCaptureSession fails when returning from background . Now when the Video is Recording and the app goes to background I want to stop recording and pause the capture session. But each time the app comes to foreground at this point I get one of the following

  1. Capture Session is not paused but recording and the Preview Layer keeps updating
  2. Capture Session provides Preview Layer with black-screen at this point app may or may not crash.

Any suggestions on handling the AVCaptureSession at this point. I would like to just show the last frame recorded on the previewLayer, once recording stops.


回答1:


I have encountered a similar situation and in my experience I have found that viewWillDisappear: doesn't get called. I'm really not sure why, but I solved it by subscribing for notifications when the app goes inactive. Here's an example:

In viewWillAppear:

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

And the appropriate 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 viewWillDisappear:

[[NSNotificationCenter defaultCenter] removeObserver:self];

I immediately move to the next view when I detect this, so I'm not sure what it leaves behind on the preview layer, but I suspect it would do what you want. Hope this helps!




回答2:


This is late but I was experiencing some of the same. In order to get around the problem you first have to realize ViewWillAppear and ViewWillDisappear are strictly for in app transitions from one View Controller to another. They don't work for foreground to background and back again transitions. I used a similar fix above:

//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];

In the selector methods just stop and start your camera session and as the other stackoverflow post suggest, it would be a good idea to lazily instantiate your avcapturesession so that your app is memory conservative



来源:https://stackoverflow.com/questions/22529465/avcapturesession-handling-after-returning-from-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!