简体   繁体   English

录制视频时切换相机?

[英]Switch camera while recording video?

So, I want to be able to switch between the front and rear camera while the video is being recorded, and without any interruption in the video stream.因此,我希望能够在录制视频时在前后摄像头之间切换,并且视频流不会中断。 I notice that even the iOS built-in camera app doesn't do this, but I've heard that some third-party apps do.我注意到即使是 iOS 内置的相机应用程序也不会这样做,但我听说一些第三方应用程序会这样做。 Below is the sample code in xamarin.ios.下面是 xamarin.ios 中的示例代码。

AVCaptureMovieFileOutput movieFileOutput;
AVCaptureDevice CurrentCamera { get; set; }
AVCaptureDevice BackCamera { get; set; }
AVCaptureDevice FrontCamera { get; set; }
AVCaptureDevice Mic { get; set; }
bool HasBackCamera { get { return BackCamera != null; } }
bool HasFrontCamera { get { return FrontCamera != null; } }
bool HasMic { get { return Mic != null; } }
void SetDeviceProperties()
{
//Set up the devices
foreach(var device in AVCaptureDevice.DevicesWithMediaType(AVMediaType.Video))
{
if(device.Position == AVCaptureDevicePosition.Back)
{
BackCamera = device;
}
else
{
FrontCamera = device;
}
}
Mic = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Audio);
}

public bool SwapCameras()
{
if(HasBackCamera && HasFrontCamera)
{
var nextCamera = CurrentCamera == BackCamera ? FrontCamera : BackCamera;
NSError error = null;
var newInput = new AVCaptureDeviceInput(nextCamera, out error);
if(error != null)
{
throw new Exception(error.ToString());
}
session.BeginConfiguration();
//Remove current video input
foreach(AVCaptureDeviceInput input in session.Inputs)
{
if(input.Device.HasMediaType(AVMediaType.Video))
{
session.RemoveInput(input);
}
}
if(session.CanAddInput(newInput))
{
session.AddInput(newInput);
}
session.CommitConfiguration();
CurrentCamera = nextCamera;
CameraConfigured(this, new TArgs<AVCaptureDevice>(CurrentCamera));
}
return CurrentCamera == FrontCamera;
}

Below is the configuration of the output of the video下面是视频输出的配置

var layer = new AVCaptureVideoPreviewLayer (session);
layer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill;
var cameraView = new UIView ();
cameraView.Layer.AddSublayer (layer);
var filePath = Path.Combine (Path.GetTempPath (), "temporary.mov");
var fileUrl = NSUrl.FromFilename (filePath);
var movieFileOutput = new AVCaptureMovieFileOutput ();
var recordingDelegate = new MyRecordingDelegate ();
session.AddOutput (movieFileOutput);
movieFileOutput.StartRecordingToOutputFile (fileUrl, recordingDelegate);

Delegate that is called when the recording is stopped(from the removeInput of the first session):录制停止时调用的委托(来自第一个会话的 removeInput):

public class MyRecordingDelegate : AVCaptureFileOutputRecordingDelegate
{
    public override void FinishedRecording (AVCaptureFileOutput captureOutput, NSUrl outputFileUrl, NSObject [] connections, NSError error)
    {
        if (UIVideo.IsCompatibleWithSavedPhotosAlbum (outputFileUrl.Path))
        {
            var library = new ALAssetsLibrary ();
            library.WriteVideoToSavedPhotosAlbum (outputFileUrl, (path, e2) =>
            {
                if (e2 != null)
                {
                    new UIAlertView ("Error", e2.ToString (), null, "OK", null).Show ();
                }
                else
                {
                    new UIAlertView ("Saved", "Saved to Photos", null, "OK", null).Show ();
                    File.Delete (outputFileUrl.Path);
                }
            });
        }
        else
        {
            new UIAlertView ("Incompatible", "Incompatible", null, "OK", null).Show ();
        }
}

}

So is this really possible and If so, how would I change the code above to not stop the recording while I switch the camera?那么这真的可能吗?如果是这样,我将如何更改上面的代码以在切换相机时不停止录制?

Once recording starts however, the swap camera icon disappears.但是,一旦开始录制,交换相机图标就会消失。 There is no way to switch between cameras while video recording is in progress.在视频录制过程中无法在摄像机之间切换。

The video must be stopped, then the camera can be switched before restarting a new recording.必须停止视频,然后才能在重新开始新录制之前切换摄像机。

As a workaround , you could merge two video into single video file with audio .作为一种解决方法,您可以将两个视频合并为带有音频的单个视频文件。 Here is a similar issue with native iOS , you could have a refer and convert the code to C# .这是本机 iOS 的类似问题,您可以参考并将代码转换为 C# 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM