Recording Using AVCaptureSession

Discussion in 'iOS Programming' started by newtoiphonesdk, Apr 27, 2012.

  1. newtoiphonesdk macrumors 6502a

    Jul 30, 2010
    I am trying to record my front Facing Camera using AVCaptureSession as used in AVCamDemo from WWDC 2010. Here is my code so far:
         AVCaptureSession *session = [[AVCaptureSession alloc] init];
        [session beginConfiguration];
    	session.sessionPreset = AVCaptureSessionPresetMedium;
    	CALayer *viewLayer = self.vImagePreview.layer;
    	NSLog(@"viewLayer = %@", viewLayer);
    	AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
        captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    	captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
    	[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
    	AVCaptureDevice *device = [self frontFacingCameraIfAvailable];
    	NSError *error = nil;
    	AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    	if (!input) {
    		// Handle the error appropriately.
    		NSLog(@"ERROR: trying to open camera: %@", error);
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectoryPath = [paths objectAtIndex:0];
        AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
        NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"];
        NSString *outputpathofmovie = [[archives stringByAppendingPathComponent:@"Test"] stringByAppendingString:@".mp4"];
        NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie];
    	[session addInput:input];
        [session commitConfiguration];
        [session startRunning];
        [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
    When I try this, I get the error:
        startRecordingToOutputFileURL:recordingDelegate:] - no active/enabled connections.
    Any thoughts?
  2. newtoiphonesdk, Apr 28, 2012
    Last edited: Apr 28, 2012

    newtoiphonesdk thread starter macrumors 6502a

    Jul 30, 2010
    My View has an MPMoviePlayerController playing, and also uses AVCaptureSession to record both audio and video. The video will record fine by itself, but when I add the input to the session for it, the record stops and video gets corrupted. If I don't use the MPMoviePlayerController, it will record audio and video fine. What can I do to make the recording work with MPMoviePlayerController running?

    I have had some slight improvement. Before, the previewlayer would always freeeze on first frame, and output of the session was 2.2 kb every time. I created a shared audio session, set mpmovieplayercontroller to use application audio session. Now, the preview layer doesn't freeze, and the video file is finally more than 2.2 kb, but it is still corrupted if using both audio and video to the session input. If one or the other, they work fine.

    Added this code:
     AVAudioSession* audioSession = [AVAudioSession sharedInstance];
        if (![audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error4]) {
            NSLog(@"AVAudioSession setCategory failed: %@", [error4 localizedDescription]);
        // Set audio session property "allow mixing" to true so audio can be recorded while it is playing
        UInt32 allowMixing = true;
        OSStatus status = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
        if (status != kAudioSessionNoError) {
            NSLog(@"AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers) failed: %ld", status);
        // Activate the audio session
        error = nil;
        if (![audioSession setActive:YES error:&error]) {
            NSLog(@"AVAudioSession setActive:YES failed: %@", [error localizedDescription]);

Share This Page