如何使用统一从网络摄像头捕获视频?

时间:2018-04-27 15:10:14

标签: unity3d webcam video-capture hololens

我正在尝试使用Unity和hololens从网络摄像头捕获视频。 我在统一页this tutorial上找到了这个例子。 我正在粘贴下面的代码。凸轮上的指示灯亮起,但它没有记录。 explode()不会创建VideoCapture。所以那里的代表从未被执行过。 我看到了here,但是已经开始了。在播放器设置上,网络摄像头和麦克风功能已打开。 可能是什么问题?

VideoCapture.CreateAsync

编辑: 问题是API无法在模拟器上运行

1 个答案:

答案 0 :(得分:1)

您应该尝试查看此帖子here。详细介绍如何使用HoloLens录制视频以及如何拍照。还要确保已设置WebCam和麦克风功能。此外,如果您尝试保存它,请确保您也具有视频库功能。

OnVideoCaptureCreated:

void OnVideoCaptureCreated (VideoCapture videoCapture)
{
   if (videoCapture != null)
   {
       m_VideoCapture = videoCapture;

       Resolution cameraResolution = VideoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
       float cameraFramerate = VideoCapture.GetSupportedFrameRatesForResolution(cameraResolution).OrderByDescending((fps) => fps).First();

       CameraParameters cameraParameters = new CameraParameters();
       cameraParameters.hologramOpacity = 0.0f;
       cameraParameters.frameRate = cameraFramerate;
       cameraParameters.cameraResolutionWidth = cameraResolution.width;
       cameraParameters.cameraResolutionHeight = cameraResolution.height;
       cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;

       m_VideoCapture.StartVideoModeAsync(cameraParameters,
                                           VideoCapture.AudioState.None,
                                           OnStartedVideoCaptureMode);
   }
   else
   {
       Debug.LogError("Failed to create VideoCapture Instance!");
   }
}

OnStartVideoCaptureMode:

void OnStartedVideoCaptureMode(VideoCapture.VideoCaptureResult result)
{
   if (result.success)
   {
       string filename = string.Format("MyVideo_{0}.mp4", Time.time);
       string filepath = System.IO.Path.Combine(Application.persistentDataPath, filename);

       m_VideoCapture.StartRecordingAsync(filepath, OnStartedRecordingVideo);
   }
}

OnStartRecordingVideo:

void OnStartedRecordingVideo(VideoCapture.VideoCaptureResult result)
{
   Debug.Log("Started Recording Video!");
   // We will stop the video from recording via other input such as a timer or a tap, etc.
}

StopRecordingVideo:

// The user has indicated to stop recording
void StopRecordingVideo()
{
   m_VideoCapture.StopRecordingAsync(OnStoppedRecordingVideo);
}

OnStopRecordingVideo:

void OnStoppedRecordingVideo(VideoCapture.VideoCaptureResult result)
{
   Debug.Log("Stopped Recording Video!");
   m_VideoCapture.StopVideoModeAsync(OnStoppedVideoCaptureMode);
}

void OnStoppedVideoCaptureMode(VideoCapture.VideoCaptureResult result)
{
   m_VideoCapture.Dispose();
   m_VideoCapture = null;
}