Saturday, January 14, 2017

AVCaptureVideoPreviewLayer Black Screen. AVfoundation Black Screen on Record

Recently in one of my project we used AVFoundation to record video. In some of the iOS devices, we were getting issue that on start recording, it shows black screen and video is not recorded. After some investigation I found out that it's because user has manually revoked camera access from settings so it was not woking. So to solve this issue, you must check if the permission is there or not. If not first request permission and if it's denied, so message to user.

So here is the function you should use. You should call this function, before you start recording and check if there is necessary permission.

- (void)requestCameraPermissionsIfNeeded {
   
    NSLog(@"requestCameraPermissionsIfNeeded");
    // check camera authorization status
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (authStatus) {
        case AVAuthorizationStatusAuthorized: { // camera authorized
            NSLog(@"requestCameraPermissionsIfNeeded camera authorized");
            // do camera intensive stuff
        }
            break;
        case AVAuthorizationStatusNotDetermined: { // request authorization
            NSLog(@"requestCameraPermissionsIfNeeded have to ask user again");
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                   
                    if(granted) {
                        // do camera intensive stuff
                    } else {
                       
                        NSLog(@"STOP RECORDING");
                        WeAreRecording = NO;
                        ShareVideo = YES;
                        [MovieFileOutput stopRecording];
                        //Prompt message to user.
                    }
                });
            }];
        }
            break;
        case AVAuthorizationStatusRestricted:{
            NSLog(@"STOP RECORDING");
            WeAreRecording = NO;
            ShareVideo = YES;
            [MovieFileOutput stopRecording];
            //Prompt message to user.
        }
           
        case AVAuthorizationStatusDenied: {
            NSLog(@"STOP RECORDING");
            WeAreRecording = NO;
            ShareVideo = YES;
            [MovieFileOutput stopRecording];
            //Prompt message to user.
            dispatch_async(dispatch_get_main_queue(), ^{
            });
        }
            break;
        default:
            break;
    }
}


This will help you in identifying issue and display proper message to user.

No comments:

Post a Comment