ios - 从后台返回后的 AVCapturesession 处理

标签 ios iphone avcapturesession

我正在使用 AVCaptureSession 实现 VideoRecorder。 我在 viewWillAppear 处启动 AVCaptureSession,并根据这个问题的建议在 viewWillDisappear 处拆除它 AVCaptureSession fails when returning from background . 现在,当视频正在录制并且应用程序进入后台时,我想停止录制并暂停捕获 session 。但是每次应用程序在此时出现在前台时,我都会得到以下内容之一

  1. Capture Session 没有暂停但正在记录并且 Preview Layer 不断更新
  2. Capture Session 提供带有黑屏的预览层,此时应用程序可能会或可能不会崩溃。

关于此时处理 AVCaptureSession 的任何建议。一旦录制停止,我只想显示在 previewLayer 上录制的最后一帧。

最佳答案

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

在 View 中会出现:

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

在 View 中会消失:

[[NSNotificationCenter defaultCenter] removeObserver:self];

当我检测到这一点时,我会立即移动到下一个 View ,所以我不确定它在预览层上留下了什么,但我怀疑它会做你想要的。希望这对您有所帮助!

关于ios - 从后台返回后的 AVCapturesession 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22529465/

相关文章:

iOS:RESTKit 与 CoreData 同步数据

ios - AVCaptureSession 图像尺寸(使用 GPUImage)

iOS AVFoundation : Get AVCaptureDevice. 格式的视频尺寸

ios - iPhone 5 上的 AVSystemController_SystemVolumeDidChangeNotification

ios - 在 iOS 11/iPhone 7/8plus 的第 3 方应用程序中使用 "portrait"模式( Bokeh )拍照?

ios - 处理消息 View

ios - 如何在 iphone iOS 中使用参数在 SQLITE 中形成 LIKE 语句?

ios - editButtonItem 不起作用

ios - 从数组中获取字典数据

ios - 简单的自定义委托(delegate)没有被调用