AVCaptureSession 在 iPad 上工作正常但在 iPhone 上不工作

AVCaptureSession working fine on iPad but not on iPhone

我正在尝试使用 AVCaptureSession 制作一个视频录制应用程序。 我有一个 UIButton,它提供预览层并按如下方式设置会话:

-(void) video:(UIButton*)sender
{
    session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    [captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    captureVideoPreviewLayer.frame = cameraView.bounds;

    [cameraView.layer addSublayer:captureVideoPreviewLayer];

    NSArray *devices = [AVCaptureDevice devices];
    AVCaptureDevice *frontCamera;
    AVCaptureDevice *backCamera;

    for (AVCaptureDevice *device in devices)
    {
        if ([device hasMediaType:AVMediaTypeVideo])
        {
            if ([device position] == AVCaptureDevicePositionBack)
                backCamera = device;
            else
                frontCamera = device;
        }
    }
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:nil];
    [session addInput:input];

    movieOutput = [[AVCaptureMovieFileOutput alloc]init];
    if ([session canAddOutput:movieOutput]) {
        [session addOutput:movieOutput];
    }

    [session startRunning];
}

然后再按一个 UIButton 开始录制:

-(void) record:(UIButton*)sender
{
    NSLog(@"Recording");

    outputURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    outputURL = [outputURL URLByAppendingPathComponent:@"myVideo.mov"];
    NSString *filePath = [outputURL path];

    [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
    NSLog(@"is url :: %d", [outputURL isFileURL]);
    NSLog(@"does file exists :: %d",[[NSFileManager defaultManager] fileExistsAtPath:filePath]);

    [movieOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];    
}

现在,我不知道为什么这段代码在 iPad 上运行正常,但在 iPhone 上崩溃并抛出错误:

'*** -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] - no active/enabled connections.'

我四处寻找针对此特定错误的解决方案,但如果我的代码有问题,那么为什么它可以在 iPad 上运行?

注:

更新: 经过如此多的反复试验,我发现如果我只添加音频作为输入,那么该应用程序在 iPhones 上运行良好。

AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
    [session addInput:audioInput];

所以问题应该出在我添加摄像头作为输入的方式上,尽管我没有找到任何摄像头。它适用于 iPad 秒。

试试下面的代码可能对你有帮助。

-(void) record:(UIButton*)sender
{
    NSLog(@"Recording");
            [[[self movieOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:(AVCaptureVideoOrientation)[UIDevice currentDevice].orientation];

    NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[@"movie" stringByAppendingPathExtension:@"mov"]];

    NSLog(@"does file exists :: %d",[[NSFileManager defaultManager] fileExistsAtPath:filePath]);

    [[self movieOutput] startRecordingToOutputFileURL:[NSURL fileURLWithPath:filePath] recordingDelegate:self];
}

见下文link

AVCAM Example

折腾了2天,发现问题是sessionPreset 属性 是AVCaptureSessionPresetPhoto。我通过删除它来解决它,这样它将使用它的默认值。