NVideoPlayer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using Nfynt.WVP;
  4. //http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4
  5. namespace Nfynt
  6. {
  7. public class NVideoPlayer : MonoBehaviour
  8. {
  9. [SerializeField, Tooltip("Video player configurations")]
  10. public PlayerConfigs Config = new PlayerConfigs()
  11. {
  12. PlayOnAwake = false,
  13. MuteAsDefault = false,
  14. LoopPlayer = false,
  15. VideoAspectRatio = UnityEngine.Video.VideoAspectRatio.Stretch
  16. };
  17. [Space(20)]
  18. public UnityEvent OnStarted;
  19. public UnityEvent OnStoped;
  20. public UnityEvent OnPaused;
  21. public UnityEvent OnResumed;
  22. public bool IsPlaying
  23. {
  24. get
  25. {
  26. if (m_videoPlayer == null) return false;
  27. return m_videoPlayer.IsPlaying();
  28. }
  29. }
  30. public bool IsMuted
  31. {
  32. get
  33. {
  34. if (m_videoPlayer == null) return false;
  35. return m_videoPlayer.IsMuted();
  36. }
  37. }
  38. public double CurrentFrameTimeInSec
  39. {
  40. get
  41. {
  42. if (m_videoPlayer == null) return 0;
  43. double time = m_videoPlayer.CurrFrameTime();
  44. return double.IsNaN(time) ? 0 : time;
  45. }
  46. }
  47. public double VideoLengthInSec
  48. {
  49. get
  50. {
  51. if (m_videoPlayer == null) return 0;
  52. double time = m_videoPlayer.VideoDuration();
  53. return double.IsNaN(time) ? 0 : time;
  54. }
  55. }
  56. public Vector2 FrameSize
  57. {
  58. get
  59. {
  60. if(m_videoPlayer==null || !m_videoPlayer.IsPlaying()) return Vector2.zero;
  61. return m_videoPlayer.FrameSize();
  62. }
  63. }
  64. private IVideoPlayer m_videoPlayer = null;
  65. private string m_videoSrc = "";
  66. private bool m_initialized = false;
  67. #region UnityFunctions
  68. private void Start()
  69. {
  70. InitializeVideoPlayer();
  71. if (m_initialized && Config.PlayOnAwake && m_videoPlayer.IsPlaying())
  72. {
  73. OnStarted?.Invoke();
  74. }
  75. }
  76. void init()
  77. {
  78. if (m_videoPlayer == null)
  79. {
  80. GameObject player = new GameObject("_videoPlayer");
  81. #if UNITY_EDITOR
  82. m_videoPlayer = player.AddComponent<UnityVideoPlayer>();
  83. #elif UNITY_WEBGL
  84. m_videoPlayer = player.AddComponent<WebVideoPlayer>();
  85. #endif
  86. player.transform.SetParent(transform);
  87. if (m_videoPlayer == null)
  88. {
  89. this.enabled = false;
  90. Debug.LogError("Current platform is not supported!");
  91. return;
  92. }
  93. if (Config.PlayOnAwake)
  94. {
  95. Debug.Log("Explicitly muting video player on start, to avoid playback error in browsers");
  96. Config.MuteAsDefault = true;
  97. }
  98. }
  99. }
  100. private void OnDestroy()
  101. {
  102. ReleaseVideoPlayer();
  103. }
  104. #endregion //unity functions
  105. private void InitializeVideoPlayer()
  106. {
  107. init();
  108. m_videoSrc = WebVideoPlayer.ValidVideoPath(Config.VideoSrcPath);
  109. if (string.IsNullOrEmpty(m_videoSrc)) m_videoSrc = Config.VideoSrcPath;
  110. if (string.IsNullOrEmpty(m_videoSrc))
  111. {
  112. Debug.LogError("Invalid video path: " + Config.VideoSrcPath);
  113. this.enabled = false;
  114. return;
  115. }
  116. Debug.Log("Initializing NVideoPlayer with src: " + m_videoSrc);
  117. Config.VideoSrcPath = m_videoSrc;
  118. if (Config.AudioSource == null)
  119. Config.AudioSource = gameObject.AddComponent<AudioSource>();
  120. m_videoPlayer.InitializePlayer(Config);
  121. m_initialized = true;
  122. }
  123. public static void ResetRenderTexture(RenderTexture renderTexture)
  124. {
  125. RenderTexture rt = RenderTexture.active;
  126. RenderTexture.active = renderTexture;
  127. GL.Clear(true, true, Color.black);
  128. RenderTexture.active = rt;
  129. }
  130. #region WVPFunctions
  131. public void Play()
  132. {
  133. if (!m_initialized) InitializeVideoPlayer();
  134. m_videoPlayer?.PlayStopVideo(Config, true);
  135. OnStarted?.Invoke();
  136. }
  137. public void Stop()
  138. {
  139. m_videoPlayer?.PlayStopVideo(Config, false);
  140. OnStoped?.Invoke();
  141. ResetRenderTexture(Config.VideoTextureTarget);
  142. }
  143. public void Pause()
  144. {
  145. m_videoPlayer?.PauseResumeVideo(true);
  146. OnPaused?.Invoke();
  147. }
  148. public void Resume()
  149. {
  150. m_videoPlayer?.PauseResumeVideo(false);
  151. OnResumed?.Invoke();
  152. }
  153. public void Mute()
  154. {
  155. m_videoPlayer?.MuteUnmuteVideo(true);
  156. }
  157. public void Unmute()
  158. {
  159. m_videoPlayer?.MuteUnmuteVideo(false);
  160. }
  161. public void ReleaseVideoPlayer()
  162. {
  163. m_videoPlayer?.ReleasePlayer();
  164. m_initialized = false;
  165. }
  166. #endregion //wvpfunctions
  167. }
  168. }