I’m working on an augmented reality app which uses an AVCaptureVideoPreviewLayer to display the video feed from the device’s camera. If you close and reopen the app (via pressing the Home button, not by force quitting) or put the phone to sleep and then wake it, the AVCaptureVideoPreviewLayer displays a black rectangle instead of the camera’s feed.
I am initializing the capture session as follows in my 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];
You will need to observe the
interruptflag of yourAVCaptureSessionusing KVO and react upon it.From the AVCaptureSession Reference:
Indicates whether the receiver has been interrupted. (read-only)