簡體   English   中英

錄制視頻時切換相機?

[英]Switch camera while recording video?

因此,我希望能夠在錄制視頻時在前后攝像頭之間切換,並且視頻流不會中斷。 我注意到即使是 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;
}

下面是視頻輸出的配置

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);

錄制停止時調用的委托(來自第一個會話的 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 ();
        }
}

}

那么這真的可能嗎?如果是這樣,我將如何更改上面的代碼以在切換相機時不停止錄制?

但是,一旦開始錄制,交換相機圖標就會消失。 在視頻錄制過程中無法在攝像機之間切換。

必須停止視頻,然后才能在重新開始新錄制之前切換攝像機。

作為一種解決方法,您可以將兩個視頻合並為帶有音頻的單個視頻文件。 這是本機 iOS 的類似問題,您可以參考並將代碼轉換為 C# 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM