【问题标题】:Toggle camera from back to front while continuing recording with Objective C使用 Objective C 继续录制时,从后到前切换相机
【发布时间】:2017-08-21 14:03:43
【问题描述】:

我创建了一个 iOS 应用程序,我可以在其中拍摄视频和照片并将它们保存到我 iPhone 上的图库中。我的问题是我想在保存之前拍摄视频时继续录制。这是最近添加到 WhatsApp 或其他应用程序的一个选项,但我不知道该怎么做。

-(BOOL)toggleCamera
{
    BOOL success=NO;

    if (self.cameraCount>1)
    {
        NSError* error;
        AVCaptureDeviceInput* newVedioInput;
        AVCaptureDevicePosition position=[self.videoInput.device position];

        if (position==AVCaptureDevicePositionBack)
        {
            newVedioInput=[[AVCaptureDeviceInput alloc] initWithDevice:self.frontFacingCamera error:&error];
        }
        else if (position==AVCaptureDevicePositionFront)
        {
            newVedioInput=[[AVCaptureDeviceInput alloc] initWithDevice:self.backFacingCamera error:&error];
        }
        else
        {
            return NO;
        }

        if (newVedioInput!=nil)
        {
            [self.session beginConfiguration];
            [self.session removeInput:self.videoInput];
            if ([self.session canAddInput:newVedioInput])
            {
                [self.session addInput:newVedioInput];
                self.videoInput=newVedioInput;
            }
            else
            {
                [self.session addInput:self.videoInput];
            }
            [self.session commitConfiguration];
            success=YES;
        }
        else if (error)
        {
            if ([self.delegate respondsToSelector:@selector(recordManager:didFailWithError:)])
            {
                [self.delegate recordManager:self didFailWithError:error];
            }
        }
    }

    return success;
}

当我这样做时,会话会发生变化,并且在翻转相机之前会保存视频,这不是我需要的。有人可以帮忙吗?

【问题讨论】:

  • 我与 AVFoundation 的合作不多,但您的解决方案可能是保存您的第一个视频的录制(即前置摄像头),然后保存您的第二个视频的录制(即后置摄像头),然后创建这两个视频的 AVComposition,将它们组合在一起。 developer.apple.com/documentation/avfoundation/avcomposition
  • 这可能是一个好主意,即使我以前没有使用过 AVComposition,如果您有更多提示,那就太好了,如果不好我会做一些研究,谢谢!跨度>
  • 据我了解,一个合成是由几种媒体类型组成的,称为AVCompositionTracks,它可以由AVAssetTracks和AVAudioTracks组成。您实际上将创建一个新的合成并指定要插入合成的 AVCompositionTracks,以及它们开始和结束的时间(直接确定每个轨道的持续时间)。您基本上可以从您制作的录音的持续时间中获得这些开始和结束时间。不过,一些额外的研究肯定会有所帮助!
  • 我在想,即使我从前到后更换相机,有没有办法继续写入同一个输出文件?
  • 我真的不确定这是否有效。我想你最终会做出一个作品来做到这一点。查看此链接stackoverflow.com/questions/18540195/…

标签: ios objective-c camera


【解决方案1】:

您不需要更改输出文件。

您需要重新配置AVCaptureSessionAVCaptureDeviceInput 输入,方法是将配置代码包含在beginConfiguration()commitConfiguration() 方法中。

使用同步将以下工作分派到会话的私有队列 方法。例如:sessionQueue.sync{}

self.session.beginConfiguration()
if let videoInput = self.videoInput {
    self.session.removeInput(videoInput)
}
addVideoInput() //adding `AVCaptureDeviceInput` again
configureSessionQuality() //Configuring session preset here.

// Fix initial frame having incorrect orientation
let connection = self.videoOutput?.connection(with: .video)
if connection?.isVideoOrientationSupported == true {
    connection?.videoOrientation = self.orientation
}

//Fix preview mirroring
if self.cameraLocation == .front {
    if connection?.isVideoMirroringSupported == true {
         connection?.isVideoMirrored = true
    }
}

self.session.commitConfiguration()

//Change zoom scale if needed.
do {
    try self.captureDevice?.lockForConfiguration()
    self.captureDevice?.videoZoomFactor = zoomScale
    self.captureDevice?.unlockForConfiguration()
} catch {
    print(error)
}

我在我的生产代码中使用上述方法。这是工作 完全没问题

【讨论】:

    猜你喜欢
    • 2015-05-04
    • 2014-04-25
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2014-07-18
    相关资源
    最近更新 更多