TemplateVideo.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using RenderHeads.Media.AVProVideo;
  5. using UnityEngine;
  6. public class TemplateVideo : BaseTemPlate
  7. {
  8. public static bool isPlayLock;
  9. public static bool isPlaying;
  10. public static Queue<TemplateVideo> temQueue = new Queue<TemplateVideo>();
  11. private Navigator.VideoControl m_VideoCtr;
  12. public AVProVideoPlayer m_Video;
  13. public string localSavePath;
  14. public string spid;
  15. public string videoid;
  16. public bool autoplay;
  17. private void OnDisable()
  18. {
  19. Debug.Log("HJJ 停止播放");
  20. m_Video.Stop();
  21. if (temQueue.Count>0)
  22. temQueue = new Queue<TemplateVideo>();
  23. }
  24. protected override void OnEnable()
  25. {
  26. //localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(Data.downloadPath);
  27. base.OnEnable();
  28. if (GameManager.Instance.IsRuning && string.IsNullOrWhiteSpace(m_Video.GetUrl()))
  29. {
  30. if (File.Exists(localSavePath))
  31. {
  32. m_Video.SetUrl(localSavePath);
  33. }
  34. else
  35. {
  36. m_Video.SetUrl(null);
  37. }
  38. }
  39. if (autoplay)
  40. {
  41. Debug.Log("HJJ 开始自动播放" + localSavePath);
  42. temQueue.Enqueue(this);
  43. // m_Video.transform.GetComponent<VideoPlayBtn>().m_IsPlaying = false;
  44. // m_Video.transform.GetComponent<VideoPlayBtn>().PlayVideo();
  45. }
  46. else
  47. {
  48. Debug.Log("HJJ 不需要自動播放" + localSavePath);
  49. }
  50. setScale = true;
  51. }
  52. // private MediaPlayer MediaPlayer;
  53. private bool setScale;
  54. protected override void OnAwake()
  55. {
  56. base.OnAwake();
  57. // m_VideoCtr = transform.Find("Screen/Video").GetComponent<Navigator.VideoControl>();
  58. m_Video = transform.Find("Screen/AVideo").GetComponent<AVProVideoPlayer>();
  59. //MediaPlayer = transform.Find("Screen/AVideo").GetComponent<MediaPlayer>();
  60. HideCollider();
  61. }
  62. private void Start()
  63. {
  64. if (!UserInfo.Instance.is20)
  65. {
  66. videoid = transform.name;
  67. spid = transform.parent.parent.name;
  68. MultiPlayerManager.Instance.OnSynVideo += OnSyncVideo;
  69. m_Video.transform.GetComponent<VideoPlayBtn>().spid = spid;
  70. m_Video.transform.GetComponent<VideoPlayBtn>().videoid = videoid;
  71. }
  72. }
  73. private void Update()
  74. {
  75. //if((MediaPlayer.Info.GetVideoHeight()!=0 || MediaPlayer.Info.GetVideoWidth()!=0) && setScale )
  76. //{
  77. // float temp = (float)MediaPlayer.Info.GetVideoHeight() / (float)MediaPlayer.Info.GetVideoWidth();
  78. // MediaPlayer.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(56,56*temp);
  79. // setScale = false;
  80. //}
  81. }
  82. public override void SetData(MaterialObjValue value, int updateTime)
  83. {
  84. base.SetData(value, updateTime);
  85. if (!GameManager.Instance.IsRuning|| Data == null)
  86. {
  87. localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  88. // DownloadManager.Instance.AddDownloadData(Data);
  89. DownLoadMaterial data = new DownLoadMaterial();
  90. data.downLoadPath = value.DownloadPath;
  91. data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  92. data.updataTime = GameManager.Instance.m_SceneValue.updateTime;
  93. data.type = "3";
  94. MsgHandler.AddListener(value.DownloadPath, HandleMsg);
  95. DownloadResManager.Instance.DownLoad(data);
  96. Debug.Log(" SetData " + updateTime);
  97. Data = data;
  98. }
  99. else
  100. {
  101. localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  102. if (File.Exists(localSavePath))
  103. {
  104. // m_VideoCtr.VideoURL = Data.localSavePath;
  105. m_Video.SetUrl(localSavePath);
  106. }
  107. else
  108. {
  109. // m_VideoCtr.VideoURL = null;
  110. m_Video.SetUrl(null);
  111. }
  112. }
  113. }
  114. public void SetData(string url)
  115. {
  116. Debug.Log(url);
  117. localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(url);
  118. //if(m_VideoCtr ==null)
  119. // m_VideoCtr = transform.Find("Screen/Video").GetComponent<Navigator.VideoControl>();
  120. //m_VideoCtr.VideoURL = url;
  121. if (m_Video == null)
  122. m_Video = transform.Find("Screen/AVideo").GetComponent<AVProVideoPlayer>();
  123. m_Video.SetUrl(localSavePath);
  124. }
  125. private void HandleMsg(Msg msg)
  126. {
  127. Debug.Log("DGJ ===> TemplateVideo");
  128. if (File.Exists(localSavePath))
  129. {
  130. // m_VideoCtr.VideoURL = Data.localSavePath;
  131. m_Video.SetUrl(localSavePath);
  132. }
  133. else
  134. {
  135. // m_VideoCtr.VideoURL = null;
  136. m_Video.SetUrl(null);
  137. }
  138. }
  139. private void OnSyncVideo(SyncVideoData syncVideo)
  140. {
  141. if(syncVideo.spid == spid&& syncVideo.id == videoid)
  142. {
  143. if (syncVideo.isPlay)
  144. m_Video.Play();
  145. else
  146. m_Video.Pause();
  147. if (syncVideo.times != -1)
  148. m_Video.SetSeek(syncVideo.times);
  149. }
  150. }
  151. public override void HideCollider()
  152. {
  153. base.HideCollider();
  154. }
  155. }