从后台返回后的AVCapturesession处理 [英] AVCapturesession handling after returning from background

查看:70
本文介绍了从后台返回后的AVCapturesession处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVCaptureSession实现VideoRecorder. 我在以下问题的建议下从viewWillAppear启动AVCaptureSession并在viewWillDisappear上将其拆散​​ AVCaptureSession在以下情况下失败从后台返回 . 现在,当正在录制视频"且应用程序进入后台时,我想停止录制并暂停捕获会话.但是,每当此应用程序出现在前台时,我都会得到以下其中之一

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. 捕获会话未暂停,但正在记录,并且预览层不断更新
  2. Capture Session在此时可能会或可能不会崩溃的情况下为预览层"提供黑屏.

有关此时处理AVCaptureSession的任何建议.录制停止后,我只想显示在PreviewLayer上录制的最后一帧.

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.

推荐答案

我遇到了类似的情况,根据我的经验,我发现viewWillDisappear:没有被调用.我真的不确定为什么,但是我通过在应用程序不活动时订阅通知来解决了这个问题.这是一个示例:

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:

在viewWillAppear中:

In viewWillAppear:

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

以及适当的回调方法:

- (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;
});
}

在viewWill中消失

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!

这篇关于从后台返回后的AVCapturesession处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆