手机解锁后AVCam预览卡住了

时间:2016-08-06 10:24:45

标签: ios objective-c avfoundation avcapturesession avcam

我们正在开发一款ios相机应用程序,其功能包括:图片/视频捕获,媒体文件管理,媒体共享等。 我们的着陆屏幕是相机预览屏幕。这有许多按钮可以导航到其他屏幕。

如果我们在取景器屏幕然后锁定并解锁手机,则预览仍然正常。但是,如果我从取景器导航到另一个屏幕并返回取景器然后进行锁定和解锁,则相机预览会卡住,即屏幕将显示最后一帧。但按钮等是可导航的。这是我们得到的错误:

Capture session runtime error: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x1490838e0 {Error Domain=NSOSStatusErrorDomain Code=-12780 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12780), NSLocalizedDescription=The operation could not be completed

查看预览的最稳定,最有效的方法是什么?

一些相关的代码片段:

   -(void) setGUIBasedOnMode{
    if (![self isStreamStarted]) {
        if (shutterActionMode == SapCamSelectionModeLiveStream)
        {
            _flashButton.hidden = true;
            _cameraButton.hidden = true;
            _liveSteamSession = [[VCSimpleSession alloc] initWithVideoSize:[[UIScreen mainScreen]bounds].size frameRate:30 bitrate:1000000 useInterfaceOrientation:YES];
            [_liveSteamSession.previewView removeFromSuperview];
            AVCaptureVideoPreviewLayer  *ptr;
            [_liveSteamSession getCameraPreviewLayer:(&ptr)];
            _liveSteamSession.previewView.frame = self.view.bounds;
            _liveSteamSession.delegate = self;
        }
        else{
            [_liveSteamSession.previewView removeFromSuperview];
            _liveSteamSession.delegate = nil;
//            [self removeObservers];
            _cameraButton.hidden = false;
            if(flashFlag == 0){
                _flashButton.hidden = false;
            }
            else if(flashFlag == 1){
                _flashButton.hidden = true;
            }
            self.session = [[AVCaptureSession alloc] init];
            self.previewView.hidden = false;
            self.previewView.session = self.session;
            [self configureCameraSettings];

            dispatch_async( self.sessionQueue, ^{
                switch ( self.setupResult )
                {
                    case AVCamSetupResultSuccess:
                    {
                        [self addObservers];
                        [self.session startRunning];

                        self.sessionRunning = self.session.isRunning;
                        if(loadingCameraFlag == false){
                            [self hidingView];
                        }
                        break;
                    }
                    case AVCamSetupResultCameraNotAuthorized:
                    {
                        dispatch_async( dispatch_get_main_queue(), ^{
                            NSString *message = NSLocalizedString( @"App doesn't have permission to use the camera, please change privacy settings", @"Alert message when the user has denied access to the camera");
                            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"AVCam" message:message preferredStyle:UIAlertControllerStyleAlert];
                            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString( @"OK", @"Alert OK button" ) style:UIAlertActionStyleCancel handler:nil];
                            [alertController addAction:cancelAction];

                            UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:NSLocalizedString( @"Settings", @"Alert button to open Settings" ) style:UIAlertActionStyleDefault handler:^( UIAlertAction *action ) {
                                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                            }];
                            [alertController addAction:settingsAction];
                            [self presentViewController:alertController animated:YES completion:nil];
                        } );
                        break;
                    }
                    case AVCamSetupResultSessionConfigurationFailed:
                    {
                        dispatch_async( dispatch_get_main_queue(), ^{
                            NSString *message = NSLocalizedString( @"Unable to capture media", @"Alert message when something goes wrong during capture session configuration" );
                            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString( @"OK", @"Alert OK button" ) style:UIAlertActionStyleCancel handler:nil];
                            [alertController addAction:cancelAction];
                            [self presentViewController:alertController animated:YES completion:nil];
                        } );
                        break;
                    }
                }
            });
        }
    }
}

更多:

[self.session beginConfiguration];

if ( [self.session canAddInput:videoDeviceInput] ) {
    [self.session addInput:videoDeviceInput];
    self.videoDeviceInput = videoDeviceInput;

    dispatch_async( dispatch_get_main_queue(), ^{
        UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;
        AVCaptureVideoOrientation initialVideoOrientation = AVCaptureVideoOrientationPortrait;
        if ( statusBarOrientation != UIInterfaceOrientationUnknown ) {
            initialVideoOrientation = (AVCaptureVideoOrientation)statusBarOrientation;
        }
        AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.previewView.layer;
        previewLayer.connection.videoOrientation = initialVideoOrientation;
    } );
}
else {
    self.setupResult = AVCamSetupResultSessionConfigurationFailed;
}

0 个答案:

没有答案