gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:08:27 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com