Change camera capture device while recording a video

前端 未结 3 442
后悔当初
后悔当初 2020-12-15 15:07

I am developing an iPhone App. In that, there is a requirement for Pausing and resuming the camera. So i used AVFoundation for that instead of using UIIma

相关标签:
3条回答
  • 2020-12-15 15:18

    Yes, you can. There are just a few of things you need to cater to.

    1. Need to be using AVCaptureVideoDataOutput and its delegate for recording.
    2. Make sure you remove the previous deviceInput before adding the new deviceInput.
    3. You must remove and recreate the AVCaptureVideoDataOutput as well.

    I am using these two functions for it right now and it works while the session is running.

    - (void)configureVideoWithDevice:(AVCaptureDevice *)camera {
    
        [_session beginConfiguration];
        [_session removeInput:_videoInputDevice];
        _videoInputDevice = nil;
    
        _videoInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:camera error:nil];
        if ([_session canAddInput:_videoInputDevice]) {
            [_session addInput:_videoInputDevice];
        }
    
        [_session removeOutput:_videoDataOutput];
        _videoDataOutput = nil;
    
        _videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
        [_videoDataOutput setSampleBufferDelegate:self queue:_outputQueueVideo];
        NSDictionary* setcapSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange], kCVPixelBufferPixelFormatTypeKey, nil];
    
        _videoDataOutput.videoSettings = setcapSettings;
        [_session addOutput:_videoDataOutput];
        _videoConnection = [_videoDataOutput connectionWithMediaType:AVMediaTypeVideo];
    
        if([_videoConnection isVideoOrientationSupported]) {
            [_videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
        }  
    
        [_session commitConfiguration];
    }
    
    - (void)configureAudioWithDevice:(AVCaptureDevice *)microphone {
        [_session beginConfiguration];
        _audioInputDevice = [AVCaptureDeviceInput deviceInputWithDevice:microphone error:nil];
        if ([_session canAddInput:_audioInputDevice]) {
            [_session addInput:_audioInputDevice];
        }
    
        [_session removeOutput:_audioDataOutput];
        _audioDataOutput = nil;
    
        _audioDataOutput = [[AVCaptureAudioDataOutput alloc] init];
        [_audioDataOutput setSampleBufferDelegate:self queue:_outputQueueAudio];
        [_session addOutput:_audioDataOutput];
        _audioConnection = [_audioDataOutput connectionWithMediaType:AVMediaTypeAudio];
    
        [_session commitConfiguration];
    }
    
    0 讨论(0)
  • 2020-12-15 15:25

    You can't change the captureDevice mid-session. And you can only have one capture session running at a time. You could end the current session and create a new one. There will be a slight lag (maybe a second or two depending on your cpu load).

    I wish Apple would allow multiple sessions or at least multiple devices per session... but they do not... yet.

    0 讨论(0)
  • 2020-12-15 15:41

    have you considered having multiple sessions and then afterwards processing the video files to join them together into one?

    0 讨论(0)
提交回复
热议问题