Hololens 录像

作者:追风剑情 发布于:2023-4-28 16:26 分类:Unity3d

using System;
using System.Collections;
using System.Linq;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Windows.WebCam;
using UnityEngine.Video;
/// <summary>
/// 录视频
/// </summary>
public class WebCamVideo : MonoBehaviour
{
    public bool showHolograms = false;
    [SerializeField]
    private VideoPlayer videoPlayer;
    [SerializeField]
    private Text debugText;

    private Resolution resolution;
    private float frameRate;
    private VideoCapture videoCapture;
    private string videoPath;

    //开始录像
    public Action OnRecordStarted;
    //录像结束事件
    public Action<string> OnRecordCompleted;
    //录像出错事件
    public Action OnRecordError;

    private void Log(string s)
    {
        Debug.Log(s);
        if (debugText != null)
            debugText.text = s;
    }

    private void LogFormat(string format, params object[] args)
    {
        Debug.LogFormat(format, args);
        if (debugText != null)
            debugText.text = string.Format(format, args);
    }

    // 获取文件存储路径
    private string GetNewFilePath()
    {
        string name = string.Format("video_{0}.mp4", DateTime.Now.ToString("yyyyMMddHHmmss"));
        string path = Path.Combine(Application.persistentDataPath, name);
        path = path.Replace("/",@"\");
        return path;
    }

    // 获取播放地址
    public string GetVideoPath()
    {
        return "file:///" + videoPath;
    }

    // 录视频
    public void TakeVideo()
    {
        StartCoroutine(StartCameraCapture());
    }

    //停止录像
    public void StopVideo()
    {
        videoCapture.StopRecordingAsync(OnStoppedRecording);
    }

    // 启动摄像头
    private IEnumerator StartCameraCapture()
    {
        //检测权限
        if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        }

        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            //第1个参数 true:视频包含全息画面,false:仅硬件摄像头画面
            VideoCapture.CreateAsync(showHolograms, OnCaptureResourceCreated);
        }
    }

    // 创建VideoCapture对象回调
    private void OnCaptureResourceCreated(VideoCapture captureObject)
    {
        videoCapture = captureObject;
        //返回最大分辨率
        resolution = VideoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
        //返回最大帧率
        frameRate = VideoCapture.GetSupportedFrameRatesForResolution(resolution).OrderByDescending((fps) => fps).First();
        LogFormat("Video Resolution: {0}x{1}", resolution.width, resolution.height);
        LogFormat("Video FrameRate: {0}fps", frameRate);

        //设置录像参数
        CameraParameters setupParams = new CameraParameters(WebCamMode.VideoMode);
        setupParams.frameRate = frameRate;
        setupParams.hologramOpacity = 0.0f;
        setupParams.cameraResolutionWidth = resolution.width;
        setupParams.cameraResolutionHeight = resolution.height;
        setupParams.pixelFormat = CapturePixelFormat.BGRA32;
        //开始启动录像模式
        videoCapture.StartVideoModeAsync(setupParams, 
            VideoCapture.AudioState.ApplicationAndMicAudio, OnVideoModeStarted);
    }

    // 摄像头启动回调
    private void OnVideoModeStarted(VideoCapture.VideoCaptureResult result)
    {
        if (!result.success)
        {
            Log("启动录像模式失败");
            OnRecordError?.Invoke();
            return;
        }
        Log("开启录像模式");

        videoPath = GetNewFilePath();
        LogFormat("Video Path: {0}", videoPath);
        //执行录像
        videoCapture.StartRecordingAsync(videoPath, OnStartedRecordingVideo);
    }

    private void OnStartedRecordingVideo(VideoCapture.VideoCaptureResult result)
    {
        Log("开始录像");
        OnRecordStarted?.Invoke();
    }

    private void OnStoppedRecording(VideoCapture.VideoCaptureResult result)
    {
        if (!result.success)
        {
            Log("无法停止录像");
            return;
        }
        Log("停止录像");
        videoCapture.StopVideoModeAsync(OnVideoModeStopped);
    }

    private void OnVideoModeStopped(VideoCapture.VideoCaptureResult result)
    {
        Log("停止录像模式,录像结束!");
        OnRecordCompleted?.Invoke(videoPath);

        if (videoPlayer != null)
        {
            videoPlayer.Stop();
            videoPlayer.url = GetVideoPath();
            videoPlayer.Play();
        }
    }
}


通过浏览器登录HoloLens后台,找到左侧菜单System->File explorer
视频文件存放地址 User Folders\LocalAppData\{应用程序完整包名}\LocalState\
111111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号