VCR.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.UI;
  6. namespace RenderHeads.Media.AVProVideo.Demos
  7. {
  8. public class VCR : MonoBehaviour
  9. {
  10. public MediaPlayer PlayingPlayer
  11. {
  12. get
  13. {
  14. if (this.LoadingPlayer == this._mediaPlayer)
  15. {
  16. return this._mediaPlayerB;
  17. }
  18. return this._mediaPlayer;
  19. }
  20. }
  21. public MediaPlayer LoadingPlayer
  22. {
  23. get
  24. {
  25. return this._loadingPlayer;
  26. }
  27. }
  28. private void SwapPlayers()
  29. {
  30. this.PlayingPlayer.Control.Pause();
  31. if (this.LoadingPlayer == this._mediaPlayer)
  32. {
  33. this._loadingPlayer = this._mediaPlayerB;
  34. }
  35. else
  36. {
  37. this._loadingPlayer = this._mediaPlayer;
  38. }
  39. this._mediaDisplay.CurrentMediaPlayer = this.PlayingPlayer;
  40. }
  41. public void OnOpenVideoFile()
  42. {
  43. this.LoadingPlayer.m_VideoPath = Path.Combine(this._folder, this._videoFiles[this._VideoIndex]);
  44. this._VideoIndex = (this._VideoIndex + 1) % this._videoFiles.Length;
  45. if (string.IsNullOrEmpty(this.LoadingPlayer.m_VideoPath))
  46. {
  47. this.LoadingPlayer.CloseVideo();
  48. this._VideoIndex = 0;
  49. }
  50. else
  51. {
  52. this.LoadingPlayer.OpenVideoFromFile(this._location, this.LoadingPlayer.m_VideoPath, this._AutoStartToggle.isOn);
  53. }
  54. }
  55. public void OnAutoStartChange()
  56. {
  57. if (this.PlayingPlayer && this._AutoStartToggle && this._AutoStartToggle.enabled && this.PlayingPlayer.m_AutoStart != this._AutoStartToggle.isOn)
  58. {
  59. this.PlayingPlayer.m_AutoStart = this._AutoStartToggle.isOn;
  60. }
  61. if (this.LoadingPlayer && this._AutoStartToggle && this._AutoStartToggle.enabled && this.LoadingPlayer.m_AutoStart != this._AutoStartToggle.isOn)
  62. {
  63. this.LoadingPlayer.m_AutoStart = this._AutoStartToggle.isOn;
  64. }
  65. }
  66. public void OnMuteChange()
  67. {
  68. if (this.PlayingPlayer)
  69. {
  70. this.PlayingPlayer.Control.MuteAudio(this._MuteToggle.isOn);
  71. }
  72. if (this.LoadingPlayer)
  73. {
  74. this.LoadingPlayer.Control.MuteAudio(this._MuteToggle.isOn);
  75. }
  76. }
  77. public void OnPlayButton()
  78. {
  79. if (this.PlayingPlayer)
  80. {
  81. this.PlayingPlayer.Control.Play();
  82. }
  83. }
  84. public void OnPauseButton()
  85. {
  86. if (this.PlayingPlayer)
  87. {
  88. this.PlayingPlayer.Control.Pause();
  89. }
  90. }
  91. public void OnVideoSeekSlider()
  92. {
  93. if (this.PlayingPlayer && this._videoSeekSlider && this._videoSeekSlider.value != this._setVideoSeekSliderValue)
  94. {
  95. this.PlayingPlayer.Control.Seek(this._videoSeekSlider.value * this.PlayingPlayer.Info.GetDurationMs());
  96. }
  97. }
  98. public void OnVideoSliderDown()
  99. {
  100. if (this.PlayingPlayer)
  101. {
  102. this._wasPlayingOnScrub = this.PlayingPlayer.Control.IsPlaying();
  103. if (this._wasPlayingOnScrub)
  104. {
  105. this.PlayingPlayer.Control.Pause();
  106. }
  107. this.OnVideoSeekSlider();
  108. }
  109. }
  110. public void OnVideoSliderUp()
  111. {
  112. if (this.PlayingPlayer && this._wasPlayingOnScrub)
  113. {
  114. this.PlayingPlayer.Control.Play();
  115. this._wasPlayingOnScrub = false;
  116. }
  117. }
  118. public void OnAudioVolumeSlider()
  119. {
  120. if (this.PlayingPlayer && this._audioVolumeSlider && this._audioVolumeSlider.value != this._setAudioVolumeSliderValue)
  121. {
  122. this.PlayingPlayer.Control.SetVolume(this._audioVolumeSlider.value);
  123. }
  124. if (this.LoadingPlayer && this._audioVolumeSlider && this._audioVolumeSlider.value != this._setAudioVolumeSliderValue)
  125. {
  126. this.LoadingPlayer.Control.SetVolume(this._audioVolumeSlider.value);
  127. }
  128. }
  129. public void OnRewindButton()
  130. {
  131. if (this.PlayingPlayer)
  132. {
  133. this.PlayingPlayer.Control.Rewind();
  134. }
  135. }
  136. private void Awake()
  137. {
  138. this._loadingPlayer = this._mediaPlayerB;
  139. }
  140. private void Start()
  141. {
  142. if (this.PlayingPlayer)
  143. {
  144. this.PlayingPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnVideoEvent));
  145. if (this.LoadingPlayer)
  146. {
  147. this.LoadingPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnVideoEvent));
  148. }
  149. if (this._audioVolumeSlider && this.PlayingPlayer.Control != null)
  150. {
  151. float volume = this.PlayingPlayer.Control.GetVolume();
  152. this._setAudioVolumeSliderValue = volume;
  153. this._audioVolumeSlider.value = volume;
  154. }
  155. this._AutoStartToggle.isOn = this.PlayingPlayer.m_AutoStart;
  156. if (this.PlayingPlayer.m_AutoOpen)
  157. {
  158. }
  159. this.OnOpenVideoFile();
  160. }
  161. }
  162. private void Update()
  163. {
  164. if (this.PlayingPlayer && this.PlayingPlayer.Info != null && this.PlayingPlayer.Info.GetDurationMs() > 0f)
  165. {
  166. float currentTimeMs = this.PlayingPlayer.Control.GetCurrentTimeMs();
  167. float num = currentTimeMs / this.PlayingPlayer.Info.GetDurationMs();
  168. this._setVideoSeekSliderValue = num;
  169. this._videoSeekSlider.value = num;
  170. }
  171. }
  172. public void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  173. {
  174. switch (et)
  175. {
  176. case MediaPlayerEvent.EventType.FirstFrameReady:
  177. this.SwapPlayers();
  178. break;
  179. }
  180. Debug.Log("Event: " + et.ToString());
  181. }
  182. public MediaPlayer _mediaPlayer;
  183. public MediaPlayer _mediaPlayerB;
  184. public DisplayUGUI _mediaDisplay;
  185. public Slider _videoSeekSlider;
  186. private float _setVideoSeekSliderValue;
  187. private bool _wasPlayingOnScrub;
  188. public Slider _audioVolumeSlider;
  189. private float _setAudioVolumeSliderValue;
  190. public Toggle _AutoStartToggle;
  191. public Toggle _MuteToggle;
  192. public MediaPlayer.FileLocation _location = MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder;
  193. public string _folder = "AVProVideoDemos/";
  194. public string[] _videoFiles = new string[]
  195. {
  196. "BigBuckBunny_720p30.mp4",
  197. "SampleSphere.mp4"
  198. };
  199. private int _VideoIndex;
  200. private MediaPlayer _loadingPlayer;
  201. }
  202. }