AVCaptureMovieFileOutput在景观中的定位

时间:2017-05-29 04:29:50

标签: ios avcapturesession avcapturedevice avcapture

我的应用使用前置摄像头录制视频。我已成功设置预览图层以在横向模式下显示它,这是应用程序运行的唯一方式。如何确保电影输出正确显示?

session = [[AVCaptureSession alloc] init];
    [session beginConfiguration];
    session.sessionPreset = AVCaptureSessionPresetMedium;

    CALayer *viewLayer = self.vImagePreview.layer;
    NSLog(@"viewLayer = %@", viewLayer);

    self.captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    self.captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
    [self.captureVideoPreviewLayer setCornerRadius:14];
    [self.captureVideoPreviewLayer setBorderWidth:3.0];
    [self.captureVideoPreviewLayer setBorderColor:[[UIColor whiteColor] CGColor]];
    self.captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
    [[vImagePreview layer] setCornerRadius:14];


    [[vImagePreview layer] setBorderWidth:3.0];


    [[vImagePreview layer] setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.vImagePreview.layer addSublayer:self.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);
    }

    AVCaptureDevice *audioCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    NSError *error2 = nil;
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevice error:&error2];





    AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];


    NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"];
    NSString *editedfilename = [[selectedCountry lastPathComponent] stringByDeletingPathExtension];
    NSString *datestring = [[editedfilename stringByAppendingString:@" "] stringByAppendingString:currentTime];
    NSLog(@"%@", datestring);
    NSString *outputpathofmovie = [[archives stringByAppendingPathComponent:datestring] stringByAppendingString:@".mp4"];
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie];
    [session addInput:input];
    [session addInput:audioInput];
    [session addOutput:movieFileOutput];
    [session commitConfiguration];
    [session startRunning];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];




    [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];

    NSLog(@"OutputURL%@", outputURL);

}
-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    switch (orientation) {
        case UIInterfaceOrientationPortrait:
            [self.captureVideoPreviewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            [self.captureVideoPreviewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
            break;
        case UIInterfaceOrientationLandscapeLeft:
            [self.captureVideoPreviewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
            break;
        case UIInterfaceOrientationLandscapeRight:
            [self.captureVideoPreviewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
            break;
    }
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {
    //finished
    NSLog(@"ErrorMessage%@", error);
}
-(IBAction)endcall {

    [player stop];
    [session stopRunning];
}
-(AVCaptureDevice *)frontFacingCameraIfAvailable
{
    NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    AVCaptureDevice *captureDevice = nil;
    for (AVCaptureDevice *device in videoDevices)
    {
        if (device.position == AVCaptureDevicePositionFront)
        {
            captureDevice = device;
            break;
        }
    }

    //  couldn't find one on the front, so just get the default video device.
    if ( ! captureDevice)
    {
        captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    }

    return captureDevice;
}

1 个答案:

答案 0 :(得分:0)

- ( void ) captureOutput: ( AVCaptureOutput * ) captureOutput

didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer       fromConnection:(AVCaptureConnection *)连接 {

connection.videoOrientation = //ur desired mode;

}

尝试实现上面的委托方法