Video Recording using AVFoundation

强颜欢笑 提交于 2019-11-29 09:23:18

问题


I am trying to record video using AVFoundation. When I add video input only to the session, everything works fine but when I add an audio input to it, it stops recording the video.(Delegate method is called immediately after recording starts). Here is my code:

-(void) recordVideo
{    
self.session = [[AVCaptureSession alloc] init];

if([session canSetSessionPreset:AVCaptureSessionPresetMedium])
    session.sessionPreset =  AVCaptureSessionPresetMedium;


CALayer *viewLayer = [self.cameraView layer];

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = viewLayer.bounds;

[viewLayer addSublayer:captureVideoPreviewLayer];



self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:[self frontFacingCameraIfAvailable] error:nil];

self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:[self audioDevice] error:nil];


if(!videoInput)
    NSLog(@"Couldn't create input!");

else
{
    self.output= [[AVCaptureMovieFileOutput alloc] init];

    NSString *pathString = [[self outputPath]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *fileURL = [NSURL fileURLWithPath:pathString];


    [session beginConfiguration];

    [session removeInput:[self videoInput]];
    if([session canAddInput:videoInput])
        [session addInput:videoInput];

    [videoInput release];

    [session removeInput:[self audioInput]];
     if([session canAddInput:audioInput])
        [session addInput:audioInput];

    [audioInput release];

    if([session canAddOutput:output])
        [session addOutput:output];

    [output release];

    [session commitConfiguration];


    [session startRunning];   

    [output startRecordingToOutputFileURL:fileURL recordingDelegate:self];
}


- (void) captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections
{
    NSLog(@"Recording Started at %@",fileURL);

}


- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
  fromConnections:(NSArray *)connections error:(NSError *)error 
{
    NSLog(@"Recording to file ended");


   [session stopRunning];
   [session release];        
}



- (AVCaptureDevice *)frontFacingCameraIfAvailable
{
   NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
   AVCaptureDevice *captureDevice = nil;

for (AVCaptureDevice *device in videoDevices)
{
    if (device.position == AVCaptureDevicePositionBack)
    {
        captureDevice = device;
        break;
    }
}    
return captureDevice;
}


- (AVCaptureDevice *) audioDevice
{
  NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
  if ([devices count] > 0) {
    return [devices objectAtIndex:0];
 }
 return nil;
}

I call [output stopRecording] after some fixed time but when I add an audio input it records a single frame and didFinishRecroding delegate method is called immediately.

Can anybody tell me whats wrong with this code.

Thanks


回答1:


I have figured it out. I needed category mixing. Following is the code that made it:

NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError]; 

if (setCategoryError) { NSLog(@"%@",[setCategoryError description]); }

 OSStatus propertySetError = 0;
 UInt32 allowMixing = true;

 propertySetError = AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (allowMixing), &allowMixing);


来源:https://stackoverflow.com/questions/6897950/video-recording-using-avfoundation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!