-1

I have already set up a session that can take photos using the AVFoundation and I have a custom camera, so I am not using the default picker controller. How can I add the option to record video?

On the button I have added a gesture that detects if it has been held and this should start the video recording. I'll post the code I have for the image capture first, and then post the code that I have tried to use for the video capture. Sorry in advance for the long post

Photo Capture Code:

(view did load)

session = [[AVCaptureSession alloc]init];
[session setSessionPreset:AVCaptureSessionPresetHigh];
inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError * error;
deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];

if([session canAddInput:deviceInput])
{
    [session addInput:deviceInput];
}


previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer * rootLayer = [myImageView layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = CGRectMake(0,0,400,800);
[previewLayer setFrame:frame];

[rootLayer insertSublayer:previewLayer atIndex:0];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary * outputSettings = [[ NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
[session startRunning];

(button method)

AVCaptureConnection * videoConnection = nil;
for(AVCaptureConnection * connection in stillImageOutput.connections)
{
    for(AVCaptureInputPort * port in [connection inputPorts])
    {
        if([[port mediaType] isEqual:AVMediaTypeVideo])
        {
            videoConnection = connection;
            break;
        }
    }
    if(videoConnection)
    {
        break;
    }
}
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
    if(imageDataSampleBuffer != NULL)
    {
        NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        UIImage * image2 = [UIImage imageWithData:imageData];
        myImageView.image= image2;
        previewLayer.hidden = YES;
        [self didFinishSelectingPhoto];
    }
}];

Video Capture Code (does not work)

- (void)doAction:(UILongPressGestureRecognizer *)recognizer {

if (recognizer.state == UIGestureRecognizerStateBegan) {

    //start video
      [session beginConfiguration];
    AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:videoCaptureDevice error:nil];



    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    [session addInput:videoInput];
    [session addOutput:movieFileOutput];

    [session commitConfiguration];
    [self startRecording];
    }
}

- (void)startRecording
{

[movieFileOutput startRecordingToOutputFileURL:[self tempFileURL]
                                    recordingDelegate:self];
}


- (void) stopRecording
{
 NSLog(@"stop recording");
[movieFileOutput stopRecording];
}

- (NSURL *) tempFileURL
{

NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath]) {
    NSLog(@"file saved");
}

return outputURL;
}
Lehman2020
  • 637
  • 7
  • 22

1 Answers1

1

Please read apple documentation and this they have clear explanation on how to capture video using AVCaptureSession. Hope this will help.

naresh
  • 627
  • 1
  • 7
  • 28