VideoTimeScrubControl.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Video;
  7. namespace Unity.VRTemplate
  8. {
  9. /// <summary>
  10. /// Connects a UI slider control to a video player, allowing users to scrub to a particular time in th video.
  11. /// </summary>
  12. [RequireComponent(typeof(VideoPlayer))]
  13. public class VideoTimeScrubControl : MonoBehaviour
  14. {
  15. [SerializeField]
  16. [Tooltip("Video play/pause button GameObject")]
  17. GameObject m_ButtonPlayOrPause;
  18. [SerializeField]
  19. [Tooltip("Slider that controls the video")]
  20. Slider m_Slider;
  21. [SerializeField]
  22. [Tooltip("Play icon sprite")]
  23. Sprite m_IconPlay;
  24. [SerializeField]
  25. [Tooltip("Pause icon sprite")]
  26. Sprite m_IconPause;
  27. [SerializeField]
  28. [Tooltip("Play or pause button image.")]
  29. Image m_ButtonPlayOrPauseIcon;
  30. [SerializeField]
  31. [Tooltip("Text that displays the current time of the video.")]
  32. TextMeshProUGUI m_VideoTimeText;
  33. [SerializeField]
  34. [Tooltip("If checked, the slider will fade off after a few seconds. If unchecked, the slider will remain on.")]
  35. bool m_HideSliderAfterFewSeconds;
  36. bool m_IsDragging;
  37. bool m_VideoIsPlaying;
  38. bool m_VideoJumpPending;
  39. long m_LastFrameBeforeScrub;
  40. VideoPlayer m_VideoPlayer;
  41. void Start()
  42. {
  43. m_VideoPlayer = GetComponent<VideoPlayer>();
  44. if (!m_VideoPlayer.playOnAwake)
  45. {
  46. m_VideoPlayer.playOnAwake = true; // Set play on awake for next enable.
  47. m_VideoPlayer.Play(); // Play video to load first frame.
  48. VideoStop(); // Stop the video to set correct state and pause frame.
  49. }
  50. else
  51. {
  52. VideoPlay(); // Play to ensure correct state.
  53. }
  54. if (m_ButtonPlayOrPause != null)
  55. m_ButtonPlayOrPause.SetActive(false);
  56. }
  57. void OnEnable()
  58. {
  59. if (m_VideoPlayer != null)
  60. {
  61. m_VideoPlayer.frame = 0;
  62. VideoPlay(); // Ensures correct UI state update if paused.
  63. }
  64. m_Slider.value = 0.0f;
  65. m_Slider.onValueChanged.AddListener(OnSliderValueChange);
  66. m_Slider.gameObject.SetActive(true);
  67. if (m_HideSliderAfterFewSeconds)
  68. StartCoroutine(HideSliderAfterSeconds());
  69. }
  70. void Update()
  71. {
  72. if (m_VideoJumpPending)
  73. {
  74. // We're trying to jump to a new position, but we're checking to make sure the video player is updated to our new jump frame.
  75. if (m_LastFrameBeforeScrub == m_VideoPlayer.frame)
  76. return;
  77. // If the video player has been updated with desired jump frame, reset these values.
  78. m_LastFrameBeforeScrub = long.MinValue;
  79. m_VideoJumpPending = false;
  80. }
  81. if (!m_IsDragging && !m_VideoJumpPending)
  82. {
  83. if (m_VideoPlayer.frameCount > 0)
  84. {
  85. var progress = (float)m_VideoPlayer.frame / m_VideoPlayer.frameCount;
  86. m_Slider.value = progress;
  87. }
  88. }
  89. }
  90. public void OnPointerDown()
  91. {
  92. m_VideoJumpPending = true;
  93. VideoStop();
  94. VideoJump();
  95. }
  96. public void OnRelease()
  97. {
  98. m_IsDragging = false;
  99. VideoPlay();
  100. VideoJump();
  101. }
  102. void OnSliderValueChange(float sliderValue)
  103. {
  104. UpdateVideoTimeText();
  105. }
  106. IEnumerator HideSliderAfterSeconds(float duration = 1f)
  107. {
  108. yield return new WaitForSeconds(duration);
  109. m_Slider.gameObject.SetActive(false);
  110. }
  111. public void OnDrag()
  112. {
  113. m_IsDragging = true;
  114. m_VideoJumpPending = true;
  115. }
  116. void VideoJump()
  117. {
  118. m_VideoJumpPending = true;
  119. var frame = m_VideoPlayer.frameCount * m_Slider.value;
  120. m_LastFrameBeforeScrub = m_VideoPlayer.frame;
  121. m_VideoPlayer.frame = (long)frame;
  122. }
  123. public void PlayOrPauseVideo()
  124. {
  125. if (m_VideoIsPlaying)
  126. {
  127. VideoStop();
  128. }
  129. else
  130. {
  131. VideoPlay();
  132. }
  133. }
  134. void UpdateVideoTimeText()
  135. {
  136. if (m_VideoPlayer != null && m_VideoTimeText != null)
  137. {
  138. var currentTimeTimeSpan = TimeSpan.FromSeconds(m_VideoPlayer.time);
  139. var totalTimeTimeSpan = TimeSpan.FromSeconds(m_VideoPlayer.length);
  140. var currentTimeString = string.Format("{0:D2}:{1:D2}",
  141. currentTimeTimeSpan.Minutes,
  142. currentTimeTimeSpan.Seconds
  143. );
  144. var totalTimeString = string.Format("{0:D2}:{1:D2}",
  145. totalTimeTimeSpan.Minutes,
  146. totalTimeTimeSpan.Seconds
  147. );
  148. m_VideoTimeText.SetText(currentTimeString + " / " + totalTimeString);
  149. }
  150. }
  151. void VideoStop()
  152. {
  153. m_VideoIsPlaying = false;
  154. m_VideoPlayer.Pause();
  155. m_ButtonPlayOrPauseIcon.sprite = m_IconPlay;
  156. m_ButtonPlayOrPause.SetActive(true);
  157. }
  158. void VideoPlay()
  159. {
  160. m_VideoIsPlaying = true;
  161. m_VideoPlayer.Play();
  162. m_ButtonPlayOrPauseIcon.sprite = m_IconPause;
  163. m_ButtonPlayOrPause.SetActive(false);
  164. }
  165. }
  166. }