ScreenCaptureDemo.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. //-----------------------------------------------------------------------------
  5. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. namespace RenderHeads.Media.AVProMovieCapture.Demos
  8. {
  9. public class ScreenCaptureDemo : MonoBehaviour
  10. {
  11. [SerializeField] AudioClip _audioBG = null;
  12. [SerializeField] AudioClip _audioHit = null;
  13. [SerializeField] float _speed = 1.0f;
  14. [SerializeField] CaptureBase _capture = null;
  15. [SerializeField] GUISkin _guiSkin = null;
  16. [SerializeField] bool _spinCamera = true;
  17. // State
  18. private float _timer;
  19. private List<FileWritingHandler> _fileWritingHandlers = new List<FileWritingHandler>(4);
  20. private IEnumerator Start()
  21. {
  22. #if UNITY_IOS
  23. Application.targetFrameRate = 60;
  24. #endif
  25. // Play music track
  26. if (_audioBG != null)
  27. {
  28. // AudioSource.PlayClipAtPoint(_audioBG, Vector3.zero);
  29. AudioSource source = gameObject.AddComponent<AudioSource>();
  30. source.clip = _audioBG;
  31. source.loop = true;
  32. source.Play();
  33. }
  34. if (_capture != null)
  35. {
  36. _capture.BeginFinalFileWritingAction += OnBeginFinalFileWriting;
  37. _capture.CompletedFileWritingAction += OnCompleteFinalFileWriting;
  38. #if (UNITY_STANDALONE_OSX || UNITY_IOS) && !UNITY_EDITOR
  39. CaptureBase.PhotoLibraryAccessLevel photoLibraryAccessLevel = CaptureBase.PhotoLibraryAccessLevel.AddOnly;
  40. // If we're trying to write to the photo library, make sure we have permission
  41. if (_capture.OutputFolder == CaptureBase.OutputPath.PhotoLibrary)
  42. {
  43. // Album creation (album name is taken from the output folder path) requires read write access.
  44. if (_capture.OutputFolderPath != null && _capture.OutputFolderPath.Length > 0)
  45. photoLibraryAccessLevel = CaptureBase.PhotoLibraryAccessLevel.ReadWrite;
  46. switch (CaptureBase.HasUserAuthorisationToAccessPhotos(photoLibraryAccessLevel))
  47. {
  48. case CaptureBase.PhotoLibraryAuthorisationStatus.Authorised:
  49. // All good, nothing to do
  50. break;
  51. case CaptureBase.PhotoLibraryAuthorisationStatus.Unavailable:
  52. Debug.LogWarning("The photo library is unavailable, will use RelativeToPeristentData instead");
  53. _capture.OutputFolder = CaptureBase.OutputPath.RelativeToPeristentData;
  54. break;
  55. case CaptureBase.PhotoLibraryAuthorisationStatus.Denied:
  56. // User has denied access, change output path
  57. Debug.LogWarning("User has denied access to the photo library, will use RelativeToPeristentData instead");
  58. _capture.OutputFolder = CaptureBase.OutputPath.RelativeToPeristentData;
  59. break;
  60. case CaptureBase.PhotoLibraryAuthorisationStatus.NotDetermined:
  61. // Need to ask permission
  62. yield return CaptureBase.RequestUserAuthorisationToAccessPhotos(photoLibraryAccessLevel);
  63. // Nested switch, everbodies favourite
  64. switch (CaptureBase.HasUserAuthorisationToAccessPhotos(photoLibraryAccessLevel))
  65. {
  66. case CaptureBase.PhotoLibraryAuthorisationStatus.Authorised:
  67. // All good, nothing to do
  68. break;
  69. case CaptureBase.PhotoLibraryAuthorisationStatus.Denied:
  70. // User has denied access, change output path
  71. Debug.LogWarning("User has denied access to the photo library, will use RelativeToPeristentData instead");
  72. _capture.OutputFolder = CaptureBase.OutputPath.RelativeToPeristentData;
  73. break;
  74. case CaptureBase.PhotoLibraryAuthorisationStatus.NotDetermined:
  75. // We were unable to request access for some reason, check the logs for any error information
  76. Debug.LogWarning("Authorisation to access the photo library is still undetermined, will use RelativeToPeristentData instead");
  77. _capture.OutputFolder = CaptureBase.OutputPath.RelativeToPeristentData;
  78. break;
  79. }
  80. break;
  81. }
  82. }
  83. #endif
  84. #if UNITY_EDITOR_OSX || (!UNITY_EDITOR && (UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_ANDROID))
  85. // Make sure we're authorised for using the microphone. On iOS the OS will forcibly
  86. // close the application if authorisation has not been granted. Make sure the
  87. // "Microphone Usage Description" field has been filled in the player settings.
  88. // Todo: handle late selection of microphone
  89. if (_capture.AudioCaptureSource == AudioCaptureSource.Microphone)
  90. {
  91. Debug.Log("Checking user has authorization to use the Microphone");
  92. switch (CaptureBase.HasUserAuthorisationToCaptureAudio())
  93. {
  94. case CaptureBase.AudioCaptureDeviceAuthorisationStatus.Unavailable:
  95. Debug.LogWarning("Audio capture is unavailable, no audio will captured");
  96. break;
  97. case CaptureBase.AudioCaptureDeviceAuthorisationStatus.NotDetermined:
  98. Debug.Log("Audio capture status is not determined, requesting access");
  99. yield return CaptureBase.RequestAudioCaptureDeviceUserAuthorisation();
  100. switch (CaptureBase.HasUserAuthorisationToCaptureAudio())
  101. {
  102. case CaptureBase.AudioCaptureDeviceAuthorisationStatus.Unavailable:
  103. Debug.LogWarning("Audio capture is unavailable, no audio will captured");
  104. break;
  105. case CaptureBase.AudioCaptureDeviceAuthorisationStatus.NotDetermined:
  106. Debug.LogWarning("Audio capture status is still not determined, no audio will captured");
  107. break;
  108. case CaptureBase.AudioCaptureDeviceAuthorisationStatus.Denied:
  109. Debug.LogWarning("Audio capture status denied, no audio will be captured");
  110. break;
  111. case CaptureBase.AudioCaptureDeviceAuthorisationStatus.Authorised:
  112. Debug.Log("Audio capture is authorised");
  113. break;
  114. }
  115. break;
  116. case CaptureBase.AudioCaptureDeviceAuthorisationStatus.Denied:
  117. Debug.LogWarning("Audio capture status denied, no audio will be captured");
  118. break;
  119. case CaptureBase.AudioCaptureDeviceAuthorisationStatus.Authorised:
  120. Debug.Log("Audio capture is authorised");
  121. break;
  122. }
  123. }
  124. #endif
  125. }
  126. yield return null;
  127. }
  128. private void OnBeginFinalFileWriting(FileWritingHandler handler)
  129. {
  130. _fileWritingHandlers.Add(handler);
  131. }
  132. private void OnCompleteFinalFileWriting(FileWritingHandler handler)
  133. {
  134. Debug.Log("Completed capture '" + handler.Path + "' with status: " + handler.Status.ToString());
  135. }
  136. private void Update()
  137. {
  138. #if (!ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER)
  139. // Press the S key to trigger audio and background color change - useful for testing A/V sync
  140. #if (UNITY_IOS || UNITY_ANDROID) && !UNITY_EDITOR
  141. bool bTouch = (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended);
  142. if (bTouch)
  143. #else
  144. if (Input.GetKeyDown(KeyCode.S))
  145. #endif
  146. {
  147. if (_audioHit != null && _capture != null && _capture.IsCapturing())
  148. {
  149. AudioSource.PlayClipAtPoint(_audioHit, Vector3.zero);
  150. Camera.main.backgroundColor = new Color(Random.value, Random.value, Random.value, 0);
  151. }
  152. }
  153. // ESC to stop capture and quit
  154. if (Input.GetKeyDown(KeyCode.Escape))
  155. {
  156. if (_capture != null && _capture.IsCapturing())
  157. {
  158. _capture.StopCapture();
  159. }
  160. else
  161. {
  162. Application.Quit();
  163. }
  164. }
  165. #endif
  166. // Spin the camera around
  167. if (_spinCamera && Camera.main != null)
  168. {
  169. Camera.main.transform.RotateAround(Vector3.zero, Vector3.up, 20f * Time.deltaTime * _speed);
  170. }
  171. if (FileWritingHandler.Cleanup(_fileWritingHandlers))
  172. {
  173. if (_fileWritingHandlers.Count == 0)
  174. {
  175. Debug.Log("All pending file writes completed");
  176. }
  177. }
  178. }
  179. void OnDestroy()
  180. {
  181. foreach (FileWritingHandler handler in _fileWritingHandlers)
  182. {
  183. handler.Dispose();
  184. }
  185. }
  186. private void OnGUI()
  187. {
  188. GUI.skin = _guiSkin;
  189. Rect r = new Rect(Screen.width - 108, 64, 128, 28);
  190. GUI.Label(r, "Frame " + Time.frameCount);
  191. }
  192. }
  193. }