UMPUIExample.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using UnityEngine.UI;
  5. namespace UMP
  6. {
  7. public class UMPUIExample : MonoBehaviour, IMediaListener, IPlayerTimeChangedListener, IPlayerPositionChangedListener
  8. {
  9. private const string BUFFERING = "BUFFERING";
  10. [SerializeField]
  11. private RawImage _videoImage = null;
  12. [SerializeField]
  13. private InputField _videoPath = null;
  14. [SerializeField]
  15. private Text _playButtonText = null;
  16. [SerializeField]
  17. private Slider _volume = null;
  18. [SerializeField]
  19. private Slider _rate = null;
  20. [SerializeField]
  21. private Text _timeText = null;
  22. [SerializeField]
  23. private Slider _rewind = null;
  24. [SerializeField]
  25. private Text _debug = null;
  26. private MediaPlayer _mediaPlayer = null;
  27. private long _mediaLength = -1;
  28. private IEnumerator _hideDebugEnumerator;
  29. void Awake()
  30. {
  31. if (_videoImage != null)
  32. {
  33. _mediaPlayer = new MediaPlayer(this, new[] { _videoImage.gameObject });
  34. // Audio output is not muted
  35. _mediaPlayer.Mute = false;
  36. // Set default audio output volume
  37. _mediaPlayer.Volume = (int)_volume.value;
  38. // Attach scecial listeners to MediaPlayer instance
  39. AddListeners();
  40. }
  41. SetDebugVisibility(false);
  42. }
  43. private void SetDebugVisibility(bool visible)
  44. {
  45. _debug.transform.parent.gameObject.SetActive(visible);
  46. _debug.gameObject.SetActive(visible);
  47. if (_hideDebugEnumerator != null)
  48. StopCoroutine(_hideDebugEnumerator);
  49. _hideDebugEnumerator = HideDebugBehaviour();
  50. StartCoroutine(_hideDebugEnumerator);
  51. }
  52. private void SetDebugText(string text)
  53. {
  54. SetDebugVisibility(true);
  55. _debug.text = text;
  56. }
  57. private IEnumerator HideDebugBehaviour()
  58. {
  59. yield return new WaitForSeconds(2.0f);
  60. if (_debug.gameObject.activeSelf)
  61. SetDebugVisibility(false);
  62. }
  63. public void OnPlayClick()
  64. {
  65. if (_mediaPlayer != null)
  66. {
  67. // Check if MediaPlayer initialiazed and have some MRL
  68. if (!_mediaPlayer.AbleToPlay)
  69. {
  70. if (!string.IsNullOrEmpty(_videoPath.text))
  71. // Set new data source for MediaPlayer as new specified Uri object
  72. _mediaPlayer.DataSource = _videoPath.text;
  73. if (_mediaPlayer.Play())
  74. _playButtonText.text = "Pause";
  75. return;
  76. }
  77. // Check if MediaPlayer is playing
  78. if (_mediaPlayer.IsPlaying)
  79. {
  80. // Set MediaPlayer to pause state
  81. _mediaPlayer.Pause();
  82. _playButtonText.text = "Play";
  83. }
  84. else
  85. {
  86. // Start play video
  87. if (_mediaPlayer.Play())
  88. _playButtonText.text = "Pause";
  89. }
  90. }
  91. }
  92. public void OnVolumeChanged()
  93. {
  94. // Set new audio output volume
  95. _mediaPlayer.Volume = (int)_volume.value;
  96. SetDebugText("Volume: " + _volume.value);
  97. }
  98. public void OnRateChanged()
  99. {
  100. // Set new video playback rate
  101. _mediaPlayer.PlaybackRate = _rate.value;
  102. SetDebugText("Playback rate: " + _rate.value);
  103. }
  104. public void OnPositionChanged()
  105. {
  106. // Set new video position
  107. _mediaPlayer.Position = _rewind.value;
  108. }
  109. public void OnStopClick()
  110. {
  111. // Stop playing current video
  112. _mediaPlayer.Stop();
  113. _mediaLength = -1;
  114. _videoPath.gameObject.SetActive(true);
  115. _playButtonText.text = "Play";
  116. SetPlayerTime(0);
  117. SetMediaLength(0);
  118. _rewind.value = 0;
  119. _rewind.enabled = false;
  120. }
  121. public void OnSnapshotClick()
  122. {
  123. // Check if MediaPlayer initialiazed and have some MRL
  124. if (!_mediaPlayer.AbleToPlay) return;
  125. if (_mediaPlayer.PlatformPlayer is MediaPlayerStandalone)
  126. {
  127. // Take a snapshot of the current video window
  128. (_mediaPlayer.PlatformPlayer as MediaPlayerStandalone).TakeSnapShot(Application.persistentDataPath);
  129. SetDebugText("Snapshot path: " + Application.persistentDataPath);
  130. }
  131. }
  132. private void OnDestroy()
  133. {
  134. if (_mediaPlayer != null)
  135. {
  136. RemoveListeners();
  137. // Release MediaPlayer
  138. _mediaPlayer.Release();
  139. }
  140. }
  141. private void AddListeners()
  142. {
  143. // Add to MediaPlayer new main group of listeners
  144. _mediaPlayer.AddMediaListener(this);
  145. // Add to MediaPlayer new "OnPlayerTimeChanged" listener
  146. _mediaPlayer.EventManager.PlayerTimeChangedListener += OnPlayerTimeChanged;
  147. // Add to MediaPlayer new "OnPlayerPositionChanged" listener
  148. _mediaPlayer.EventManager.PlayerPositionChangedListener += OnPlayerPositionChanged;
  149. }
  150. private void RemoveListeners()
  151. {
  152. // Remove from MediaPlayer the main group of listeners
  153. _mediaPlayer.RemoveMediaListener(this);
  154. // Remove from MediaPlayer "OnPlayerTimeChanged" listener
  155. _mediaPlayer.EventManager.PlayerTimeChangedListener -= OnPlayerTimeChanged;
  156. // Remove from MediaPlayer "OnPlayerPositionChanged" listener
  157. _mediaPlayer.EventManager.PlayerPositionChangedListener -= OnPlayerPositionChanged;
  158. }
  159. void SetPlayerTime(long playedTime)
  160. {
  161. var time = TimeSpan.FromMilliseconds(playedTime);
  162. string text = _timeText.text;
  163. int separatorIndex = text.IndexOf("\n", StringComparison.Ordinal);
  164. _timeText.text = string.Format("{0:D2}:{1:D2}:{2:D2}", time.Hours, time.Minutes, time.Seconds) + text.Substring(separatorIndex);
  165. }
  166. void SetMediaLength(long mediaLength)
  167. {
  168. var length = TimeSpan.FromMilliseconds(mediaLength);
  169. string text = _timeText.text;
  170. int separatorIndex = text.IndexOf("\n", StringComparison.Ordinal);
  171. _timeText.text = text.Substring(0, separatorIndex + 1) + string.Format("{0:D2}:{1:D2}:{2:D2}", length.Hours, length.Minutes, length.Seconds);
  172. }
  173. public void OnPlayerOpening()
  174. {
  175. _videoPath.gameObject.SetActive(false);
  176. Debug.Log("Opening");
  177. }
  178. public void OnPlayerBuffering(float percentage)
  179. {
  180. if (!_debug.text.Contains(BUFFERING))
  181. _debug.text = BUFFERING + ": " + percentage + "%";
  182. SetDebugText(BUFFERING + ": " + percentage + "%");
  183. Debug.Log("Buffering: (" + percentage + "%)");
  184. }
  185. public void OnPlayerImageReady(Texture2D image)
  186. {
  187. Debug.Log("ImageReady:( " + image.width + ", " + image.height + ")");
  188. }
  189. public void OnPlayerPrepared(int videoWidth, int videoHeight)
  190. {
  191. Debug.Log("Prepared:( " + videoWidth + ", " + videoHeight + ")");
  192. }
  193. public void OnPlayerPlaying()
  194. {
  195. if (_mediaLength < 0)
  196. {
  197. _mediaLength = _mediaPlayer.Length;
  198. SetMediaLength(_mediaLength);
  199. }
  200. _rewind.enabled = true;
  201. Debug.Log("Playing");
  202. }
  203. public void OnPlayerPaused()
  204. {
  205. Debug.Log("Paused");
  206. }
  207. public void OnPlayerStopped()
  208. {
  209. Debug.Log("Stopped");
  210. }
  211. public void OnPlayerEndReached()
  212. {
  213. OnStopClick();
  214. Debug.Log("OnPlayerEndReached");
  215. }
  216. public void OnPlayerEncounteredError()
  217. {
  218. OnStopClick();
  219. if (_mediaPlayer.PlatformPlayer is MediaPlayerStandalone)
  220. Debug.Log((_mediaPlayer.PlatformPlayer as MediaPlayerStandalone).GetLastError());
  221. }
  222. public void OnPlayerTimeChanged(long time)
  223. {
  224. SetPlayerTime(time);
  225. }
  226. public void OnPlayerPositionChanged(float position)
  227. {
  228. _rewind.value = position;
  229. }
  230. }
  231. }