单击开始录制时如何以相同的时间间隔开始录制视频?

时间:2018-11-10 09:38:55

标签: ios objective-c swift video avcapturesession

我已经通过avcapturesession实现了视频和音频录制。通常大约需要1到2秒钟才能开始录制。

如果需要更多信息,请随意发表评论。 设置捕获会话执行以下步骤:

  • 添加捕获会话
  • 添加视频输入
  • 添加音频输入
  • 添加视频预览层
  • 添加电影输出
  • 设置摄像机配置以进行输出连接
  • 开始捕获会话

    -(void)setupCaptureSession {

     // Set up capture session
         captureSession = [AVCaptureSession new];
    
    //add video intput
    AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
       if (videoCaptureDevice) {
        NSError *error;
    
        videoInputDevice =  [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
        if(!error){
            if ([captureSession canAddInput:videoInputDevice]) {
                [captureSession addInput:videoInputDevice];
            }else{
                NSLog(@"Failed to add video input");
            }
        }else{
            NSLog(@"Failed to create video device");
        }
    }else{
        NSLog(@"Failed to create video capture device");
    }
    
    // add audio input
    AVCaptureDevice *audioCaptureDevie = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    
    NSError *error = nil;
    
    AVCaptureDeviceInput *audioInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:audioCaptureDevie error:&error];
    if (audioInputDevice) {
        [captureSession addInput:audioInputDevice];
    }
    
    // add outputs
    
    // add video preview layer
    NSLog(@"Adding video preview layer");
    
    [self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession]];
    
    [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    
    //add movie ouput
    movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
    movieFileOutput.minFreeDiskSpaceLimit = 1024 * 1024;
    
    if ([captureSession canAddOutput:movieFileOutput])
        [captureSession addOutput:movieFileOutput];
    
    // set up camera configuration for output connection
    [self setUpCameraConfiguration];
    
    [captureSession setSessionPreset:AVCaptureSessionPresetMedium];
    if ([captureSession canSetSessionPreset:AVCaptureSessionPreset640x480])
        [captureSession setSessionPreset:AVCaptureSessionPreset640x480];
    
    
    
    //----- DISPLAY THE PREVIEW LAYER -----
    //Display it full screen under out view controller existing controls
    NSLog(@"Display the preview layer");
    CGRect layerRect = [[[self view] layer] bounds];
    [_previewLayer setBounds:layerRect];
    [_previewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),
                                           CGRectGetMidY(layerRect))];
    //[[[self view] layer] addSublayer:[[self CaptureManager] previewLayer]];
    //We use this instead so it goes on a layer behind our UI controls (avoids us having to manually bring each control to the front):
    UIView *cameraView = [[UIView alloc] init];
    [[self view] addSubview:cameraView];
    [self.view sendSubviewToBack:cameraView];
    
    [[cameraView layer] addSublayer:_previewLayer];
    
    
    //----- START THE CAPTURE SESSION RUNNING -----
    [captureSession startRunning];
    

    }

0 个答案:

没有答案