AVCaptureSession didFinishRecordingToOutputFileAtURL:在中断(例如控制中心)时不调用

时间:2015-10-22 17:45:01

标签: ios objective-c avcapturesession

我沉溺了很多时间,所以我想我会转发。

我正在使用AVCaptureSession和AVCaptureMovieFileOutput来允许用户在提供暂停录制按钮的同时录制视频。

这种方法很有效,直到应用程序背景,控制中心被抬起,或者发生警报/呼叫等中断。根据设备硬件的不同,我们会看到一种不同的错误。

  • 在较旧的设备上,即使我们重新初始化captureSession,捕获会话也无法在此类事件后开始录制。
  • 在较新的设备上,现有视频(如果存在)可以正常使用,但任何后续尝试恢复录制都将彻底失败,或无法录制音频。

在第一种情况下,didStartRecordingToOutputFileAtURL:永远不会被调用。在第二种情况下,didStartRecordingToOutputFileAtURL:将被调用,但didFinishRecordingToOutputFileAtURL:将不会。

目前,我们正在这样做:

- (void)viewDidLoad {
   [self initializeCaptureSessionAndOutputData]; //based on your requirements
}

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
   [self.captureSession startRunning];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruptionDidOccur)
                                               name:UIApplicationWillResignActiveNotification
                                           object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
   [super viewWillDisappear:animated];
   [self.captureSession stopRunning];
   [[NSNotificationCenter] defaultCenter] removeObserver:self];
}

- (void)interruptionDidOccur {
   [self.movieFileOutput stopRecording]; //expect didFinishRecordingToOutputFileAtURL to be called
}

1 个答案:

答案 0 :(得分:0)

问题没有记录,但可以解决。除了打电话

[self.captureSession stopRunning]

viewWillDisappear中的

:每当触发UIApplicationWillResignActiveNotification通知时也必须调用它。以下是示例代码:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self handleAppReturn];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(becameActive)
                                             name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignActive)
                                             name:UIApplicationWillResignActiveNotification
                                           object:nil];
}

- (void)resignActive {
    [self.videoCameraManager.captureSession stopRunning];
    if (!self.isPaused) {
       [self pauseVideo];
    }
}

- (void)becameActive {
    [self.videoCameraManager.captureSession startRunning];
}