UnityVideoPlayer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using UnityEngine;
  2. using UnityEngine.Video;
  3. namespace Nfynt.WVP
  4. {
  5. [RequireComponent(typeof(VideoPlayer))]
  6. internal class UnityVideoPlayer : MonoBehaviour, IVideoPlayer
  7. {
  8. private VideoPlayer m_player = null;
  9. private const int AudioTrack = 0;
  10. public void Awake()
  11. {
  12. m_player = gameObject.GetComponent<VideoPlayer>() ?? gameObject.AddComponent<VideoPlayer>();
  13. m_player.source = VideoSource.Url;
  14. m_player.playOnAwake = false;
  15. m_player.isLooping = false;
  16. m_player.renderMode = VideoRenderMode.RenderTexture;
  17. m_player.aspectRatio = VideoAspectRatio.FitInside;
  18. }
  19. private void UpdateConfig(PlayerConfigs config)
  20. {
  21. m_player.source = VideoSource.Url;
  22. m_player.url = config.VideoSrcPath;
  23. m_player.targetTexture = config.VideoTextureTarget;
  24. m_player.audioOutputMode = VideoAudioOutputMode.AudioSource;
  25. config.AudioSource.mute = config.MuteAsDefault;
  26. m_player.SetTargetAudioSource(AudioTrack, config.AudioSource);
  27. m_player.isLooping = config.LoopPlayer;
  28. m_player.playOnAwake = config.PlayOnAwake;
  29. m_player.aspectRatio = config.VideoAspectRatio;
  30. Debug.Log("Video aspect: " + m_player.aspectRatio);
  31. }
  32. public void InitializePlayer(PlayerConfigs config)
  33. {
  34. UpdateConfig(config);
  35. if (config.PlayOnAwake)
  36. m_player.Play();
  37. }
  38. public void ReleasePlayer() { }
  39. public bool PlayStopVideo(PlayerConfigs config, bool play)
  40. {
  41. if (m_player == null) return false;
  42. if (play)
  43. {
  44. UpdateConfig(config);
  45. m_player.Play();
  46. }
  47. else
  48. m_player.Stop();
  49. return true;
  50. }
  51. public bool PauseResumeVideo(bool pause)
  52. {
  53. if (m_player == null) return false;
  54. if (pause)
  55. m_player.Pause();
  56. else
  57. m_player.Play();
  58. return true;
  59. }
  60. public bool MuteUnmuteVideo(bool mute)
  61. {
  62. if (m_player == null) return false;
  63. AudioSource audSrc = m_player.GetTargetAudioSource(AudioTrack);
  64. if (audSrc==null) return false;
  65. audSrc.mute = mute;
  66. return true;
  67. }
  68. public bool IsPlaying()
  69. {
  70. if (m_player == null) return false;
  71. return m_player.isPlaying;
  72. }
  73. public bool IsMuted()
  74. {
  75. if(m_player==null) return false;
  76. return m_player.GetTargetAudioSource(AudioTrack).mute;
  77. }
  78. public double VideoDuration()
  79. {
  80. if(m_player == null) return 0;
  81. if (m_player.source == VideoSource.VideoClip) return m_player.clip.length;
  82. if(!m_player.isPrepared) return 0;
  83. double time = m_player.frameCount / m_player.frameRate;
  84. return time;
  85. }
  86. public double CurrFrameTime()
  87. {
  88. if(m_player== null) return 0;
  89. return m_player.time;
  90. }
  91. public bool SetFrameTime(double timeInSec)
  92. {
  93. if(m_player==null) return false;
  94. m_player.time = timeInSec;
  95. return true;
  96. }
  97. public Vector2 FrameSize()
  98. {
  99. if (m_player==null) return Vector2.zero;
  100. return new Vector2(m_player.width, m_player.height);
  101. }
  102. }
  103. }