在录制视频时切换前后摄像头

4
我创建了一个自定义相机,并尝试添加一个功能,使用户在录制视频时可以切换前后摄像头。我目前的方法是在他们切换摄像头时停止并开始新的录制,但它会截断一小部分视频,我不确定原因。如何让它像 Snapchat 一样,获得完整的视频,在他们切换摄像头时不截断任何内容。以下是我的代码:
@objc func switchCameraInput() {
        self.captureSession.beginConfiguration()
        var existingConnection:AVCaptureDeviceInput!

        for connection in self.captureSession.inputs {
            let input = connection as! AVCaptureDeviceInput
            if input.device.hasMediaType(AVMediaType.video) {
                existingConnection = input
            }
        }

        self.captureSession.removeInput(existingConnection)
        turnFlashOff()

        var newCamera:AVCaptureDevice!
        if let oldCamera = existingConnection {
            if oldCamera.device.position == .back {
                newCamera = self.cameraWithPosition(position: .front)
            } else {
                newCamera = self.cameraWithPosition(position: .back)
            }
        }

        var newInput:AVCaptureDeviceInput!

        do {
            newInput = try AVCaptureDeviceInput(device: newCamera)
            self.captureSession.addInput(newInput)

        } catch {
            ProgressHUD.showError(error.localizedDescription)
        }


        self.captureSession.commitConfiguration()

    // This is where i handle switching while recording
    if self.movieFileOutput.isRecording {
        hasSwappedCamera = true
        turnFlashOff()
        //self.movieFileOutput.stopRecording()
        self.movieFileOutput.connection(with: AVMediaType.video)?.videoOrientation = self.videoOrientation()
        self.movieFileOutput.maxRecordedDuration = self.maxRecordedDuration()
        self.movieFileOutput.startRecording(to: URL(fileURLWithPath:self.videoFileLocation()), recordingDelegate: self)
        turnOnFlash()
    }
}

看起来这个问题可能会对你有所帮助...https://dev59.com/9KLia4cB1Zd3GeqPmKE_ - BHendricks
@BHendricks 这是Objective-C,你能帮我用Swift吗? - vApp
2个回答

2

由于您提出的问题所涉及的回答是用Objective-C编写的,而您更喜欢Swift,因此我在下面“翻译”了所有代码。

请注意,我没有编译这段代码,并且知道有几个地方一开始就无法编译。例如,Enums值如AVMediaTypeVideo在Swift中通常只是.video。此外,我相信该回答中存在一些不正确的代码,主要涉及将isFrontRecordingisBackRecording布尔值重置为false。我认为这些操作应该发生在completionHandler中,但是如上所述,我没有编译这段代码,因此请谨慎对待。我包含了那个问题(Objective-C)的所有代码以及我快速而粗略的Swift翻译。

希望这能帮到您 :)

Objective-C:

/* Front camera settings */
@property bool isFrontRecording;
@property (strong, nonatomic) AVCaptureDeviceInput *videoInputBack;
@property (strong, nonatomic) AVCaptureStillImageOutput *imageOutputBack;
@property (strong, nonatomic) AVCaptureSession *sessionBack;

/* Back camera settings */
@property bool isBackRecording;
@property (strong, nonatomic) AVCaptureDeviceInput *videoInputFront;
@property (strong, nonatomic) AVCaptureStillImageOutput *imageOutputFront;
@property (strong, nonatomic) AVCaptureSession *sessionFront;

Swift:

var isFrontRecording: Bool
var videoInputBack: AVCaptureDeviceInput
var imageOutputBack: AVCaptureStillImageOutput
var sessionBack: AVCaptureSession

var isBackRecording: Bool
var videoInputFront: AVCaptureDeviceInput
var imageOutputFront: AVCaptureStillImageOutput
var sessionFront: AVCaptureSession

Objective-C

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupBackAVCapture];

    self.isFrontRecording = NO;
    self.isBackRecording = NO;
}

- (void)setupBackAVCapture
{
    NSError *error = nil;

    self.sessionBack = [[AVCaptureSession alloc] init];
    self.sessionBack.sessionPreset = AVCaptureSessionPresetPhoto;

    AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    self.videoInputBack = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];
    [self.sessionBack addInput:self.videoInputBack];

    self.imageOutputBack = [[AVCaptureStillImageOutput alloc] init];
    [self.sessionBack addOutput:self.imageOutputBack];

}

Swift:

override func viewDidLoad() {
    super.viewDidLoad()
    setupBackAVCapture()

    isFrontRecording = false
    isBackRecording = false
}

func setupBackAVCapture() {
    var error: NSError = nil
    sessionBack = AVCaptureSession()
    sessionBack.sessionPreset = AVCaptureSessionPresetPhoto

    let camera: AVCaptureDevice = AVCaptureDevice(defaultDeviceWithMediaType: AVMediaTypeVideo) 
    videoInputBack = AVCaptureDeviceInput(withDevice: camera, error: error)
    sessionBack.addInput(videoInputBack)

    imageOutputBack = AVCaptureStillImageOutput()
    sessionBack.addOutput(imageOutputBack)
}

Objective-C:

- (IBAction)buttonCapture:(id)sender {
    [self takeBackPhoto];
}

- (void)takeBackPhoto
{
    [self.sessionBack startRunning];
    if (!self.isFrontRecording) {

        self.isFrontRecording = YES;

        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        AVCaptureConnection *videoConnection = [self.imageOutputBack connectionWithMediaType:AVMediaTypeVideo];

        if (videoConnection == nil) {
            return;
        }


        [self.imageOutputBack
         captureStillImageAsynchronouslyFromConnection:videoConnection
         completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

             if (imageDataSampleBuffer == NULL) {
                 return;
             }

             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

             UIImage *image = [[UIImage alloc] initWithData:imageData];

             UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

             [self.imageView setImage:image];

             [self.sessionBack stopRunning];

             // Set up front camera setting and capture photo.
             [self setupFrontAVCapture];
             [self takeFrontPhoto];

         }];

        self.isFrontRecording = NO;
    }
}

Swift:

@IBOutlet func buttonCapture(sender: Any) {
    takeBackPhoto()
}

func takeBackPhoto() {
    sessionBack.startRunning()
    if !isFrontRecording {
        isFrontRecording = true

        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
        let videoConnection: AVCaptureConnection = imageOutputBack.connectionWithMediaType(AVMediaTypeVideo)

        guard let videoConnection = videoConnection else {
            return
        }

        imageOutputBack.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
            imageDataSampleBuffer: CMSSampleBufferRef, error: NSError in

            guard let imageDataSampleBuffer = imageDataSampleBuffer else {
                return
            }

            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
            let image = UIImage(data: imageData)
            UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
            self.imageView.setImage(image)
            self.sessionback.stopRunning()

            // Set up front camera setting and capture photo.
            self.setupFronAVCapture()
            self.takeFrontPhoto()
        })

        isFrontRecording = false
    }
}

Objective-C

- (void)setupFrontAVCapture
{
    NSError *error = nil;

    self.sessionFront = [[AVCaptureSession alloc] init];
    self.sessionFront.sessionPreset = AVCaptureSessionPresetPhoto;

    AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    camera = [self cameraWithPosition:AVCaptureDevicePositionFront];

    self.videoInputFront = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];
    [self.sessionFront addInput:self.videoInputFront];

    self.imageOutputFront = [[AVCaptureStillImageOutput alloc] init];
    [self.sessionFront addOutput:self.imageOutputFront];
}

- (void)takeFrontPhoto
{
    [self.sessionFront startRunning];
    if (!self.isBackRecording) {

        self.isBackRecording = YES;

        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        AVCaptureConnection *videoConnection = [self.imageOutputFront connectionWithMediaType:AVMediaTypeVideo];

        if (videoConnection == nil) {
            return;
        }


        [self.imageOutputFront
         captureStillImageAsynchronouslyFromConnection:videoConnection
         completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

             if (imageDataSampleBuffer == NULL) {
                 return;
             }

             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

             UIImage *image = [[UIImage alloc] initWithData:imageData];

             UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
             [self.imageViewBack setImage:image];

             [self.sessionFront stopRunning];


         }];

        self.isBackRecording = NO;

    }

}

Swift:

func setupFrontAVCapture() {
    let error: NSError = nil
    sessionFront = AVCaptureSession()
    sessionFront.sessionPreset = AVCaptureSessionPresentPhoto

    var camera: AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    camera = camera.cameraWithPosition(AVCaptureDevicePositionFront)

    videoInputFront = AVCaptureDeviceInput(withDevice: camera, error: error)
    sessionFront.addInput(videoInputFront)

    imageOutputFront = AVCaptureStillImageOutput()
    sessionFront.addOutput(imageOutputFront)
}

func takeFrontPhoto() {
    sessionFront.startRunning()

    if !isBackRecording {
        isBackRecording = true

        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
        let videoConnection: AVCaptureConnection = imageOutputFront.connectionWithMediaType(AVMediaTypeVideo)

        guard let videoConnection = videoConnection else {
            return
        }

        imageOutputFront.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
            imageDataSampleBuffer: CMSampleBufferRef, error: NSError in

            guard let imageDataSampleBuffer = imageDataSampleBuffer else {
                return
            }

            let imageData: NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
            let image = UIImage(data: imageData)

            UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
            self.imageViewBack.setImage(image)
            self.sessionFront.stopRunning()
        })

        isBackRecording = false
    }
}

祝你在项目中成功实现切换!


1
它适用于静态图像,但在录制视频时切换相机仍然不清晰。 - Piyush Mathur

0
我找到了一个适合这个完全相同问题的解决方案,而不是用一个捕获会话和一个输出来切换输入,你必须为每个输入(相机)创建一个会话,然后在它们之间切换输出。
你可以在这里找到更多详细信息:https://dev59.com/OWzXa4cB1Zd3GeqPRkT3#54770398

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接