123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using UnityEngine;
- using UnityEngine.Events;
- using Nfynt.WVP;
- //http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4
- namespace Nfynt
- {
- public class NVideoPlayer : MonoBehaviour
- {
- [SerializeField, Tooltip("Video player configurations")]
- public PlayerConfigs Config = new PlayerConfigs()
- {
- PlayOnAwake = false,
- MuteAsDefault = false,
- LoopPlayer = false,
- VideoAspectRatio = UnityEngine.Video.VideoAspectRatio.Stretch
- };
- [Space(20)]
- public UnityEvent OnStarted;
- public UnityEvent OnStoped;
- public UnityEvent OnPaused;
- public UnityEvent OnResumed;
- public bool IsPlaying
- {
- get
- {
- if (m_videoPlayer == null) return false;
- return m_videoPlayer.IsPlaying();
- }
- }
- public bool IsMuted
- {
- get
- {
- if (m_videoPlayer == null) return false;
- return m_videoPlayer.IsMuted();
- }
- }
- public double CurrentFrameTimeInSec
- {
- get
- {
- if (m_videoPlayer == null) return 0;
- double time = m_videoPlayer.CurrFrameTime();
- return double.IsNaN(time) ? 0 : time;
- }
- }
- public double VideoLengthInSec
- {
- get
- {
- if (m_videoPlayer == null) return 0;
- double time = m_videoPlayer.VideoDuration();
- return double.IsNaN(time) ? 0 : time;
- }
- }
- public Vector2 FrameSize
- {
- get
- {
- if(m_videoPlayer==null || !m_videoPlayer.IsPlaying()) return Vector2.zero;
- return m_videoPlayer.FrameSize();
- }
- }
- private IVideoPlayer m_videoPlayer = null;
- private string m_videoSrc = "";
- private bool m_initialized = false;
- #region UnityFunctions
- private void Start()
- {
- InitializeVideoPlayer();
- if (m_initialized && Config.PlayOnAwake && m_videoPlayer.IsPlaying())
- {
- OnStarted?.Invoke();
- }
- }
- void init()
- {
- if (m_videoPlayer == null)
- {
- GameObject player = new GameObject("_videoPlayer");
- #if UNITY_EDITOR
- m_videoPlayer = player.AddComponent<UnityVideoPlayer>();
- #elif UNITY_WEBGL
- m_videoPlayer = player.AddComponent<WebVideoPlayer>();
- #endif
- player.transform.SetParent(transform);
- if (m_videoPlayer == null)
- {
- this.enabled = false;
- Debug.LogError("Current platform is not supported!");
- return;
- }
- if (Config.PlayOnAwake)
- {
- Debug.Log("Explicitly muting video player on start, to avoid playback error in browsers");
- Config.MuteAsDefault = true;
- }
- }
- }
- private void OnDestroy()
- {
- ReleaseVideoPlayer();
- }
- #endregion //unity functions
- private void InitializeVideoPlayer()
- {
- init();
- m_videoSrc = WebVideoPlayer.ValidVideoPath(Config.VideoSrcPath);
- if (string.IsNullOrEmpty(m_videoSrc)) m_videoSrc = Config.VideoSrcPath;
- if (string.IsNullOrEmpty(m_videoSrc))
- {
- Debug.LogError("Invalid video path: " + Config.VideoSrcPath);
- this.enabled = false;
- return;
- }
- Debug.Log("Initializing NVideoPlayer with src: " + m_videoSrc);
- Config.VideoSrcPath = m_videoSrc;
- if (Config.AudioSource == null)
- Config.AudioSource = gameObject.AddComponent<AudioSource>();
-
- m_videoPlayer.InitializePlayer(Config);
- m_initialized = true;
- }
- public static void ResetRenderTexture(RenderTexture renderTexture)
- {
- RenderTexture rt = RenderTexture.active;
- RenderTexture.active = renderTexture;
- GL.Clear(true, true, Color.black);
- RenderTexture.active = rt;
- }
- #region WVPFunctions
- public void Play()
- {
- if (!m_initialized) InitializeVideoPlayer();
- m_videoPlayer?.PlayStopVideo(Config, true);
- OnStarted?.Invoke();
- }
- public void Stop()
- {
- m_videoPlayer?.PlayStopVideo(Config, false);
- OnStoped?.Invoke();
- ResetRenderTexture(Config.VideoTextureTarget);
- }
- public void Pause()
- {
- m_videoPlayer?.PauseResumeVideo(true);
- OnPaused?.Invoke();
- }
- public void Resume()
- {
- m_videoPlayer?.PauseResumeVideo(false);
- OnResumed?.Invoke();
- }
- public void Mute()
- {
- m_videoPlayer?.MuteUnmuteVideo(true);
- }
- public void Unmute()
- {
- m_videoPlayer?.MuteUnmuteVideo(false);
- }
- public void ReleaseVideoPlayer()
- {
- m_videoPlayer?.ReleasePlayer();
- m_initialized = false;
- }
- #endregion //wvpfunctions
- }
- }
|