Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using NatCorder;
  5. using NatCorder.Clocks;
  6. using System;
  7. using UnityEngine.UI;
  8. using UnityEngine.Video;
  9.  
  10. public class VideoRecorder : MonoBehaviour
  11. {
  12. [SerializeField]
  13. private Text DebugMode;
  14. [SerializeField]
  15. private GameObject box;
  16. [SerializeField]
  17. private RawImage m_videoTexture;
  18. [SerializeField]
  19. private VideoPlayer m_videoPlayer;
  20.  
  21. private WebCamTexture webcamTexture;
  22. private RealtimeClock clock;
  23. private MP4Recorder mediaRecorder;
  24.  
  25. void Update()
  26. {
  27. // Check that we are recording
  28. if (webcamTexture.didUpdateThisFrame)
  29. {
  30. box.transform.Rotate(0, 15, 0);
  31.  
  32. if (mediaRecorder != null)
  33. {
  34. mediaRecorder.CommitFrame(webcamTexture.GetPixels32(), clock.Timestamp); // Commit the frame to the recorder
  35. }
  36. }
  37. }
  38.  
  39. public void StartRecording()
  40. {
  41. Debug.Log("Recording started");
  42. DebugMode.text += "Recording started..." + "\n";
  43.  
  44. // Start the webcam texture
  45. webcamTexture = new WebCamTexture();
  46. webcamTexture.Play();
  47.  
  48. // Start recording
  49. clock = new RealtimeClock();
  50. mediaRecorder = new MP4Recorder(Screen.width, Screen.height, 60, 0, 0, OnRecordingFinish);
  51. //mediaRecorder.CommitFrame(webcamTexture.GetPixels(), clock.Timestamp);
  52. }
  53.  
  54. IEnumerator PlayVideo()
  55. {
  56. DebugMode.text += "Video is preparing to play";
  57. m_videoPlayer.Prepare();
  58.  
  59. while (!m_videoPlayer.isPrepared)
  60. {
  61. yield return null;
  62. }
  63.  
  64. m_videoPlayer.Play();
  65. DebugMode.text += "Video is playing";
  66. m_videoTexture.texture = m_videoPlayer.texture;
  67. }
  68.  
  69. void OnRecordingFinish(string videoPath)
  70. {
  71. DebugMode.text += ("Recording finished" + "\n");
  72. DebugMode.text += "Video path is: " + videoPath;
  73.  
  74. m_videoPlayer.url = videoPath;
  75. StartCoroutine(PlayVideo());
  76.  
  77. }
  78.  
  79. public void StopRecording()
  80. {
  81. // Stop recording
  82. mediaRecorder.Dispose();
  83. mediaRecorder = null;
  84. DebugMode.text += ("Recording stopped" + "\n");
  85.  
  86. }
  87.  
  88. public void PlayAgain()
  89. {
  90. StartCoroutine(PlayVideo());
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement