Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using NatCorder;
- using NatCorder.Clocks;
- using System;
- using UnityEngine.UI;
- using UnityEngine.Video;
- public class VideoRecorder : MonoBehaviour
- {
- [SerializeField]
- private Text DebugMode;
- [SerializeField]
- private GameObject box;
- [SerializeField]
- private RawImage m_videoTexture;
- [SerializeField]
- private VideoPlayer m_videoPlayer;
- private WebCamTexture webcamTexture;
- private RealtimeClock clock;
- private MP4Recorder mediaRecorder;
- void Update()
- {
- // Check that we are recording
- if (webcamTexture.didUpdateThisFrame)
- {
- box.transform.Rotate(0, 15, 0);
- if (mediaRecorder != null)
- {
- mediaRecorder.CommitFrame(webcamTexture.GetPixels32(), clock.Timestamp); // Commit the frame to the recorder
- }
- }
- }
- public void StartRecording()
- {
- Debug.Log("Recording started");
- DebugMode.text += "Recording started..." + "\n";
- // Start the webcam texture
- webcamTexture = new WebCamTexture();
- webcamTexture.Play();
- // Start recording
- clock = new RealtimeClock();
- mediaRecorder = new MP4Recorder(Screen.width, Screen.height, 60, 0, 0, OnRecordingFinish);
- //mediaRecorder.CommitFrame(webcamTexture.GetPixels(), clock.Timestamp);
- }
- IEnumerator PlayVideo()
- {
- DebugMode.text += "Video is preparing to play";
- m_videoPlayer.Prepare();
- while (!m_videoPlayer.isPrepared)
- {
- yield return null;
- }
- m_videoPlayer.Play();
- DebugMode.text += "Video is playing";
- m_videoTexture.texture = m_videoPlayer.texture;
- }
- void OnRecordingFinish(string videoPath)
- {
- DebugMode.text += ("Recording finished" + "\n");
- DebugMode.text += "Video path is: " + videoPath;
- m_videoPlayer.url = videoPath;
- StartCoroutine(PlayVideo());
- }
- public void StopRecording()
- {
- // Stop recording
- mediaRecorder.Dispose();
- mediaRecorder = null;
- DebugMode.text += ("Recording stopped" + "\n");
- }
- public void PlayAgain()
- {
- StartCoroutine(PlayVideo());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement