PlaybackSync.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using UnityEngine;
  3. namespace RenderHeads.Media.AVProVideo.Demos
  4. {
  5. public class PlaybackSync : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. for (int i = 0; i < this._slavePlayers.Length; i++)
  10. {
  11. this._slavePlayers[i].m_Muted = true;
  12. if (this._matchVideo)
  13. {
  14. this._slavePlayers[i].OpenVideoFromFile(this._masterPlayer.m_VideoLocation, this._masterPlayer.m_VideoPath, false);
  15. }
  16. }
  17. }
  18. private void LateUpdate()
  19. {
  20. if (this._state == PlaybackSync.State.Loading && this.IsAllVideosLoaded())
  21. {
  22. this._masterPlayer.Play();
  23. for (int i = 0; i < this._slavePlayers.Length; i++)
  24. {
  25. this._slavePlayers[i].Play();
  26. }
  27. this._state = PlaybackSync.State.Playing;
  28. }
  29. if (this._state == PlaybackSync.State.Finished)
  30. {
  31. Debug.Log("Do Something");
  32. }
  33. else if (this._state == PlaybackSync.State.Playing)
  34. {
  35. if (this._masterPlayer.Control.IsPlaying())
  36. {
  37. float currentTimeMs = this._masterPlayer.Control.GetCurrentTimeMs();
  38. for (int j = 0; j < this._slavePlayers.Length; j++)
  39. {
  40. MediaPlayer mediaPlayer = this._slavePlayers[j];
  41. float currentTimeMs2 = mediaPlayer.Control.GetCurrentTimeMs();
  42. float num = Mathf.Abs(currentTimeMs - currentTimeMs2);
  43. if (num > this._toleranceMs)
  44. {
  45. mediaPlayer.Control.SeekFast(currentTimeMs + this._toleranceMs * 0.5f);
  46. if (mediaPlayer.Control.IsPaused())
  47. {
  48. mediaPlayer.Play();
  49. }
  50. }
  51. }
  52. }
  53. else
  54. {
  55. for (int k = 0; k < this._slavePlayers.Length; k++)
  56. {
  57. MediaPlayer mediaPlayer2 = this._slavePlayers[k];
  58. mediaPlayer2.Pause();
  59. }
  60. }
  61. if (PlaybackSync.IsPlaybackFinished(this._masterPlayer))
  62. {
  63. this._state = PlaybackSync.State.Finished;
  64. }
  65. }
  66. }
  67. private bool IsAllVideosLoaded()
  68. {
  69. bool result = false;
  70. if (PlaybackSync.IsVideoLoaded(this._masterPlayer))
  71. {
  72. result = true;
  73. for (int i = 0; i < this._slavePlayers.Length; i++)
  74. {
  75. if (!PlaybackSync.IsVideoLoaded(this._slavePlayers[i]))
  76. {
  77. result = false;
  78. break;
  79. }
  80. }
  81. }
  82. return result;
  83. }
  84. private static bool IsVideoLoaded(MediaPlayer player)
  85. {
  86. return player != null && player.Control != null && player.Control.HasMetaData() && player.Control.CanPlay() && player.TextureProducer.GetTextureFrameCount() > 0;
  87. }
  88. private static bool IsPlaybackFinished(MediaPlayer player)
  89. {
  90. bool result = false;
  91. if (player != null && player.Control != null && player.Control.IsFinished())
  92. {
  93. result = true;
  94. }
  95. return result;
  96. }
  97. public MediaPlayer _masterPlayer;
  98. public MediaPlayer[] _slavePlayers;
  99. public float _toleranceMs = 30f;
  100. public bool _matchVideo = true;
  101. public bool _muteSlaves = true;
  102. private PlaybackSync.State _state;
  103. private enum State
  104. {
  105. Loading,
  106. Playing,
  107. Finished
  108. }
  109. }
  110. }