SimpleController.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. namespace RenderHeads.Media.AVProVideo.Demos
  8. {
  9. public class SimpleController : MonoBehaviour
  10. {
  11. private void Start()
  12. {
  13. this._mediaPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
  14. }
  15. public void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  16. {
  17. switch (et)
  18. {
  19. case MediaPlayerEvent.EventType.MetaDataReady:
  20. this.GatherProperties();
  21. break;
  22. }
  23. this.AddEvent(et);
  24. }
  25. private void AddEvent(MediaPlayerEvent.EventType et)
  26. {
  27. Debug.Log("[SimpleController] Event: " + et.ToString());
  28. this._eventLog.Enqueue(et.ToString());
  29. if (this._eventLog.Count > 5)
  30. {
  31. this._eventLog.Dequeue();
  32. this._eventTimer = 1f;
  33. }
  34. }
  35. private void GatherProperties()
  36. {
  37. if (this._mediaPlayer != null && this._mediaPlayer.Info != null)
  38. {
  39. this._width = this._mediaPlayer.Info.GetVideoWidth();
  40. this._height = this._mediaPlayer.Info.GetVideoHeight();
  41. this._durationSeconds = this._mediaPlayer.Info.GetDurationMs() / 1000f;
  42. }
  43. }
  44. private void Update()
  45. {
  46. if (!this._useFading && this._display != null && this._display._mediaPlayer != null && this._display._mediaPlayer.Control != null)
  47. {
  48. this._display._color = Color.white;
  49. this._display._mediaPlayer.Control.SetVolume(1f);
  50. }
  51. if (this._eventLog != null && this._eventLog.Count > 0)
  52. {
  53. this._eventTimer -= Time.deltaTime;
  54. if (this._eventTimer < 0f)
  55. {
  56. this._eventLog.Dequeue();
  57. this._eventTimer = 1f;
  58. }
  59. }
  60. }
  61. private void LoadVideo(string filePath, bool url = false)
  62. {
  63. if (!url)
  64. {
  65. this._nextVideoLocation = MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder;
  66. }
  67. else
  68. {
  69. this._nextVideoLocation = MediaPlayer.FileLocation.AbsolutePathOrURL;
  70. }
  71. this._nextVideoPath = filePath;
  72. if (!this._useFading)
  73. {
  74. if (!this._mediaPlayer.OpenVideoFromFile(this._nextVideoLocation, this._nextVideoPath, this._mediaPlayer.m_AutoStart))
  75. {
  76. Debug.LogError("Failed to open video!");
  77. }
  78. }
  79. else
  80. {
  81. base.StartCoroutine("LoadVideoWithFading");
  82. }
  83. }
  84. private static bool VideoIsReady(MediaPlayer mp)
  85. {
  86. return mp != null && mp.TextureProducer != null && mp.TextureProducer.GetTextureFrameCount() <= 0;
  87. }
  88. private static bool AudioIsReady(MediaPlayer mp)
  89. {
  90. return mp != null && mp.Control != null && mp.Control.CanPlay() && mp.Info.HasAudio() && !mp.Info.HasVideo();
  91. }
  92. private IEnumerator LoadVideoWithFading()
  93. {
  94. float fade = 0.25f;
  95. while (fade > 0f && Application.isPlaying)
  96. {
  97. fade -= Time.deltaTime;
  98. fade = Mathf.Clamp(fade, 0f, 0.25f);
  99. this._display._color = new Color(1f, 1f, 1f, fade / 0.25f);
  100. this._display._mediaPlayer.Control.SetVolume(fade / 0.25f);
  101. yield return null;
  102. }
  103. yield return new WaitForEndOfFrame();
  104. yield return new WaitForEndOfFrame();
  105. yield return new WaitForEndOfFrame();
  106. if (Application.isPlaying)
  107. {
  108. if (!this._mediaPlayer.OpenVideoFromFile(this._nextVideoLocation, this._nextVideoPath, this._mediaPlayer.m_AutoStart))
  109. {
  110. Debug.LogError("Failed to open video!");
  111. }
  112. else
  113. {
  114. while (Application.isPlaying && (SimpleController.VideoIsReady(this._mediaPlayer) || SimpleController.AudioIsReady(this._mediaPlayer)))
  115. {
  116. yield return null;
  117. }
  118. yield return new WaitForEndOfFrame();
  119. yield return new WaitForEndOfFrame();
  120. yield return new WaitForEndOfFrame();
  121. }
  122. }
  123. while (fade < 0.25f && Application.isPlaying)
  124. {
  125. fade += Time.deltaTime;
  126. fade = Mathf.Clamp(fade, 0f, 0.25f);
  127. this._display._color = new Color(1f, 1f, 1f, fade / 0.25f);
  128. this._display._mediaPlayer.Control.SetVolume(fade / 0.25f);
  129. yield return null;
  130. }
  131. yield break;
  132. }
  133. private void OnGUI()
  134. {
  135. if (this._mediaPlayer == null)
  136. {
  137. return;
  138. }
  139. GUI.depth = -10;
  140. if (this._guiSkin != null)
  141. {
  142. GUI.skin = this._guiSkin;
  143. }
  144. GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3((float)Screen.width / 960f, (float)Screen.height / 540f, 1f));
  145. GUILayout.BeginVertical("box", new GUILayoutOption[0]);
  146. if (this._mediaPlayer.Control != null)
  147. {
  148. GUILayout.Label("Loaded: " + this._mediaPlayer.m_VideoPath, new GUILayoutOption[0]);
  149. GUILayout.Label(string.Format("Size: {0}x{1} FPS: {3} Duration: {2}ms", new object[]
  150. {
  151. this._width,
  152. this._height,
  153. this._mediaPlayer.Info.GetDurationMs(),
  154. this._mediaPlayer.Info.GetVideoFrameRate().ToString("F2")
  155. }), new GUILayoutOption[0]);
  156. GUILayout.Label(string.Concat(new object[]
  157. {
  158. "Updates: ",
  159. this._mediaPlayer.TextureProducer.GetTextureFrameCount(),
  160. " Rate: ",
  161. this._mediaPlayer.Info.GetVideoDisplayRate().ToString("F1")
  162. }), new GUILayoutOption[0]);
  163. GUILayout.BeginHorizontal(new GUILayoutOption[0]);
  164. this._useFading = GUILayout.Toggle(this._useFading, "Fade to Black During Loading", new GUILayoutOption[0]);
  165. this._mediaPlayer.m_AutoStart = GUILayout.Toggle(this._mediaPlayer.m_AutoStart, "Auto Play After Load", new GUILayoutOption[0]);
  166. bool loop = this._mediaPlayer.m_Loop;
  167. bool flag = GUILayout.Toggle(loop, "Loop", new GUILayoutOption[0]);
  168. if (flag != loop)
  169. {
  170. this._mediaPlayer.m_Loop = flag;
  171. this._mediaPlayer.Control.SetLooping(flag);
  172. }
  173. GUILayout.EndHorizontal();
  174. int num = (int)this._mediaPlayer.Control.GetCurrentTimeMs();
  175. int num2 = (int)GUILayout.HorizontalSlider((float)num, 0f, this._durationSeconds * 1000f, new GUILayoutOption[0]);
  176. Rect lastRect = GUILayoutUtility.GetLastRect();
  177. float x = GUI.skin.horizontalSliderThumb.CalcSize(GUIContent.none).x;
  178. Rect position = lastRect;
  179. GUI.color = Color.green;
  180. position.xMin += x;
  181. position.y = position.yMax - 4f;
  182. position.width -= x * 1f;
  183. position.width *= this._mediaPlayer.Control.GetBufferingProgress();
  184. position.height = 4f;
  185. GUI.DrawTexture(position, Texture2D.whiteTexture, ScaleMode.StretchToFill);
  186. GUI.color = Color.green;
  187. int bufferedTimeRangeCount = this._mediaPlayer.Control.GetBufferedTimeRangeCount();
  188. for (int i = 0; i < bufferedTimeRangeCount; i++)
  189. {
  190. float num3 = 0f;
  191. float num4 = 0f;
  192. if (this._mediaPlayer.Control.GetBufferedTimeRange(i, ref num3, ref num4))
  193. {
  194. position.xMin = x + lastRect.x + (lastRect.width - x * 1f) * (num3 / (this._durationSeconds * 1000f));
  195. position.xMax = x + lastRect.x + (lastRect.width - x * 1f) * (num4 / (this._durationSeconds * 1000f));
  196. GUI.DrawTexture(position, Texture2D.whiteTexture, ScaleMode.StretchToFill);
  197. }
  198. }
  199. GUI.color = Color.white;
  200. if (num2 != num)
  201. {
  202. this._mediaPlayer.Control.Seek((float)num2);
  203. }
  204. if (!this._mediaPlayer.Control.IsPlaying())
  205. {
  206. if (GUILayout.Button("Play", new GUILayoutOption[0]))
  207. {
  208. this._mediaPlayer.Control.Play();
  209. }
  210. }
  211. else if (GUILayout.Button("Pause", new GUILayoutOption[0]))
  212. {
  213. this._mediaPlayer.Control.Pause();
  214. }
  215. GUILayout.BeginHorizontal(new GUILayoutOption[0]);
  216. int audioTrackCount = this._mediaPlayer.Info.GetAudioTrackCount();
  217. int currentAudioTrack = this._mediaPlayer.Control.GetCurrentAudioTrack();
  218. for (int j = 0; j < audioTrackCount; j++)
  219. {
  220. if (j == currentAudioTrack)
  221. {
  222. GUI.color = Color.green;
  223. }
  224. if (GUILayout.Button("Audio Track #" + (j + 1), new GUILayoutOption[0]))
  225. {
  226. this._mediaPlayer.Control.SetAudioTrack(j);
  227. }
  228. GUI.color = Color.white;
  229. }
  230. GUILayout.EndHorizontal();
  231. }
  232. GUILayout.Label("Select a new file to play:", new GUILayoutOption[0]);
  233. int num5 = GUILayout.SelectionGrid(-1, this._filenames, 3, new GUILayoutOption[0]);
  234. if (num5 >= 0)
  235. {
  236. this.LoadVideo(Path.Combine(this._folder, this._filenames[num5]), false);
  237. }
  238. GUILayout.Space(8f);
  239. GUILayout.Label("Select a new stream to play:", new GUILayoutOption[0]);
  240. int num6 = GUILayout.SelectionGrid(-1, this._streams, 1, new GUILayoutOption[0]);
  241. if (num6 >= 0)
  242. {
  243. this.LoadVideo(this._streams[num6], true);
  244. }
  245. GUILayout.Space(8f);
  246. GUILayout.Label("Recent Events: ", new GUILayoutOption[0]);
  247. GUILayout.BeginVertical("box", new GUILayoutOption[0]);
  248. int num7 = 0;
  249. foreach (string text in this._eventLog)
  250. {
  251. GUI.color = Color.white;
  252. if (num7 == 0)
  253. {
  254. GUI.color = new Color(1f, 1f, 1f, this._eventTimer);
  255. }
  256. GUILayout.Label(text, new GUILayoutOption[0]);
  257. num7++;
  258. }
  259. GUILayout.EndVertical();
  260. GUI.color = Color.white;
  261. GUILayout.EndVertical();
  262. }
  263. public string _folder = "AVProVideoSamples/";
  264. public string[] _filenames = new string[]
  265. {
  266. "SampleSphere.mp4",
  267. "BigBuckBunny_360p30.mp3",
  268. "BigBuckBunny_720p30.mp4"
  269. };
  270. public string[] _streams;
  271. public MediaPlayer _mediaPlayer;
  272. public DisplayIMGUI _display;
  273. public GUISkin _guiSkin;
  274. private int _width;
  275. private int _height;
  276. private float _durationSeconds;
  277. public bool _useFading = true;
  278. private Queue<string> _eventLog = new Queue<string>(8);
  279. private float _eventTimer = 1f;
  280. private MediaPlayer.FileLocation _nextVideoLocation;
  281. private string _nextVideoPath;
  282. }
  283. }