StartEndPoint.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using UnityEngine;
  3. namespace RenderHeads.Media.AVProVideo.Demos
  4. {
  5. public class StartEndPoint : MonoBehaviour
  6. {
  7. private void OnEnable()
  8. {
  9. this._isStartQueued = true;
  10. }
  11. private void Update()
  12. {
  13. if (StartEndPoint.IsVideoLoaded(this._mediaPlayer))
  14. {
  15. if (this._isStartQueued)
  16. {
  17. StartEndPoint.DoStart(this._mediaPlayer, this._startPointSeconds);
  18. this._isStartQueued = false;
  19. }
  20. else if (!this._loop)
  21. {
  22. StartEndPoint.DoCheckEnd(this._mediaPlayer, this._endPointSeconds);
  23. }
  24. else
  25. {
  26. StartEndPoint.DoCheckLoop(this._mediaPlayer, this._endPointSeconds, this._startLoopSeconds);
  27. }
  28. }
  29. }
  30. private static bool IsVideoLoaded(MediaPlayer mp)
  31. {
  32. return mp != null && mp.Control != null && mp.Control.HasMetaData();
  33. }
  34. private static void DoStart(MediaPlayer mp, float positionSeconds)
  35. {
  36. mp.Control.Seek(positionSeconds * 1000f);
  37. mp.Play();
  38. }
  39. private static void DoCheckEnd(MediaPlayer mp, float positionSeconds)
  40. {
  41. if (mp.Control.IsPlaying() && mp.Control.GetCurrentTimeMs() >= positionSeconds * 1000f)
  42. {
  43. mp.Pause();
  44. }
  45. }
  46. private static void DoCheckLoop(MediaPlayer mp, float positionSeconds, float positionLoop)
  47. {
  48. if (mp.Control.IsPlaying() && mp.Control.GetCurrentTimeMs() >= positionSeconds * 1000f)
  49. {
  50. mp.Control.Seek(positionLoop * 1000f);
  51. }
  52. }
  53. public MediaPlayer _mediaPlayer;
  54. public float _startPointSeconds;
  55. public float _endPointSeconds;
  56. public bool _loop;
  57. [Tooltip("If looping is enabled, this is the time the video rewinds to when it reaches the end point")]
  58. public float _startLoopSeconds;
  59. private bool _isStartQueued;
  60. }
  61. }