1

我正在开发一个增强现实应用程序,它使用AVCaptureVideoPreviewLayer来显示来自设备摄像头的视频源。如果您关闭并重新打开应用程序(通过按下主页按钮,而不是强制退出)或让手机进入睡眠状态然后唤醒它,则会AVCaptureVideoPreviewLayer显示一个黑色矩形而不是相机的提要。

我正在初始化捕获会话,如下所示viewDidLoad:

captureSession = [[AVCaptureSession alloc] init];

AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice) { 
    NSError *error;
    AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
    if (!error) {
        if ([captureSession canAddInput:videoIn])
            [captureSession addInput:videoIn];
        else
            NSLog(@"Couldn't add video input");
    } else
        NSLog(@"Couldn't create video input");
} else
    NSLog(@"Couldn't create video capture device");

[captureSession startRunning];

AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
previewLayer.frame = cameraView.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[[cameraView layer] addSublayer:previewLayer];
4

1 回答 1

0

您将需要观察您使用 KVO的interrupt标志并对其做出反应。AVCaptureSession

AVCaptureSession 参考

interrupted

指示接收器是否已被中断。(只读)

@property(nonatomic, readonly, getter=isInterrupted) BOOL interrupted
Discussion

您可以使用键值观察来观察此属性的值

在 iOS 4.0 及更高版本中可用。在 AVCaptureSession.h 中声明

于 2012-04-12T14:25:07.503 回答