使用AVFoundation进行视频录制

8

我正在尝试使用AVFoundation录制视频。当我将只有视频输入的会话添加进去时,一切都正常,但是当我加入音频输入时,它就停止了录制视频。(在录制开始后立即调用Delegate方法)。以下是我的代码:

-(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;
}

在一定时间后,我调用[output stopRecording],但当我添加音频输入时,它只记录了一个帧,然后立即调用didFinishRecroding委托方法。 有人能告诉我这个代码有什么问题吗?
谢谢

委托函数中是否有任何错误?您能否使用 NSLog(@"%@", error);,并在有错误的情况下将其发布在此处? - Mihai Fratu
错误域= AVFoundationErrorDomain 代码=-11818 "录制已停止" 用户信息=0x197dd0 {NSLocalizedRecoverySuggestion=请停止使用录制设备的任何其他操作,然后重试。, NSLocalizedDescription=录制已停止} - shujaat
只是想补充一下,我也正在使用MPMoviePlayercontroller播放视频,在同一个屏幕上预览视频录制过程。但每次都会出现错误 "Error Domain=AVFoundationErrorDomain Code=-11818 "Recording Stopped" UserInfo=0x197dd0 {NSLocalizedRecoverySuggestion=Stop any other actions using the recording device and try again., NSLocalizedDescription=Recording Stopped}"。但是当我禁用视频播放,只录制视频时,一切都正常运行。请帮帮我,谢谢! - shujaat
大家好!有人有什么想法吗?MPMoviePlayerController和AvFoundation之间有冲突吗?还是其他问题? - shujaat
你好,能否提供一下[self videoInput]、[self videoInput]和[self audioInput]这些方法?实际上我正在尝试做这个:http://stackoverflow.com/questions/16014431/how-can-i-record-video-without-opening-video-camera。 - Dhara
对于Swift-4.2,请参考以下答案:https://dev59.com/tJTfa4cB1Zd3GeqPVMLz#57109568 - Rubaiyat Jahan Mumu
1个回答

7

我已经想通了。我需要混合分类。以下是实现此目的的代码:

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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接