iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 使用AVFoundation进行视频录制大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用AVFoundation录制视频.当我只将视频输入添加到会话时,一切正常,但是当我向其添加音频输入时,它会停止录制视频.(录制开始后立即调用Delegate方法).这是我的代码: @H_489_5@ @H_489_5@
-(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;
}
@H_489_5@我在一段固定的时间之后调用[output stopRecording],但是当我添加一个音频输入时,它会记录一个帧并立即调用didFinishRecroding委托方法.

@H_489_5@任何人都可以告诉我这个代码有什么问题.

@H_489_5@谢谢

解决方法

我已经弄清楚了.我需要分类混合.以下是制作它的代码: @H_489_5@ @H_489_5@
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);

大佬总结

以上是大佬教程为你收集整理的iphone – 使用AVFoundation进行视频录制全部内容,希望文章能够帮你解决iphone – 使用AVFoundation进行视频录制所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。