TemplateVideo.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  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. TemplateVideo.isPlaying = false;
  20. Debug.Log("HJJ ֹͣ����");
  21. m_Video.Stop();
  22. if (temQueue.Count>0)
  23. temQueue = new Queue<TemplateVideo>();
  24. }
  25. protected override void OnEnable()
  26. {
  27. //localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(Data.downloadPath);
  28. base.OnEnable();
  29. if (GameManager.Instance.IsRuning && string.IsNullOrWhiteSpace(m_Video.GetUrl()))
  30. {
  31. if (File.Exists(localSavePath))
  32. {
  33. m_Video.SetUrl(localSavePath);
  34. }
  35. else
  36. {
  37. m_Video.SetUrl(null);
  38. }
  39. }
  40. GameManager.Instance.text.text += localSavePath + "\n";
  41. if (autoplay)
  42. {
  43. Debug.Log("HJJ ��ʼ�Զ�����" + localSavePath);
  44. StartCoroutine(ADDAutoPlay());
  45. // m_Video.transform.GetComponent<VideoPlayBtn>().m_IsPlaying = false;
  46. // m_Video.transform.GetComponent<VideoPlayBtn>().PlayVideo();
  47. }
  48. else
  49. {
  50. Debug.Log("HJJ ����Ҫ�ԄӲ���" + localSavePath);
  51. }
  52. setScale = true;
  53. }
  54. IEnumerator ADDAutoPlay()
  55. {
  56. yield return new WaitForSeconds(0.3f);
  57. Debug.Log("HJJ ��ʼ�Զ����ż��딵�M" + localSavePath);
  58. temQueue.Enqueue(this);
  59. }
  60. // private MediaPlayer MediaPlayer;
  61. private bool setScale;
  62. protected override void OnAwake()
  63. {
  64. base.OnAwake();
  65. // m_VideoCtr = transform.Find("Screen/Video").GetComponent<Navigator.VideoControl>();
  66. m_Video = transform.Find("Screen/AVideo").GetComponent<AVProVideoPlayer>();
  67. //MediaPlayer = transform.Find("Screen/AVideo").GetComponent<MediaPlayer>();
  68. HideCollider();
  69. }
  70. private void Start()
  71. {
  72. if (!UserInfo.Instance.is20)
  73. {
  74. videoid = transform.name;
  75. spid = transform.parent.parent.name;
  76. MultiPlayerManager.Instance.OnSynVideo += OnSyncVideo;
  77. m_Video.transform.GetComponent<VideoPlayBtn>().spid = spid;
  78. m_Video.transform.GetComponent<VideoPlayBtn>().videoid = videoid;
  79. }
  80. mRawImage = transform.Find("Screen/Video").GetComponent<RawImage>();
  81. }
  82. private RawImage mRawImage;
  83. private void Update()
  84. {
  85. //if((MediaPlayer.Info.GetVideoHeight()!=0 || MediaPlayer.Info.GetVideoWidth()!=0) && setScale )
  86. //{
  87. // float temp = (float)MediaPlayer.Info.GetVideoHeight() / (float)MediaPlayer.Info.GetVideoWidth();
  88. // MediaPlayer.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(56,56*temp);
  89. // setScale = false;
  90. //}
  91. // ��Ƶԭʼ����
  92. if(mRawImage.texture!=null && setScale)
  93. {
  94. mRawImage.transform.parent.GetComponent<RectTransform>().localScale = new Vector3(mRawImage.transform.parent.GetComponent<RectTransform>().localScale.x, mRawImage.transform.parent.GetComponent<RectTransform>().localScale.x,mRawImage.transform.parent.GetComponent<RectTransform>().localScale.z);
  95. float temp = (float)mRawImage.texture.height / (float)mRawImage.texture.width;
  96. if(mRawImage.texture.height>mRawImage.texture.width)
  97. mRawImage.GetComponent<RectTransform>().sizeDelta = new Vector2(1.5f/temp,1.5f);
  98. else
  99. mRawImage.GetComponent<RectTransform>().sizeDelta = new Vector2(2,2*temp);
  100. setScale = false;
  101. }
  102. }
  103. public override void SetData(MaterialObjValue value, int updateTime)
  104. {
  105. base.SetData(value, updateTime);
  106. if (!GameManager.Instance.IsRuning|| Data == null)
  107. {
  108. localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  109. // DownloadManager.Instance.AddDownloadData(Data);
  110. DownLoadMaterial data = new DownLoadMaterial();
  111. data.downLoadPath = value.DownloadPath;
  112. data.localLoadPath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  113. data.updataTime = GameManager.Instance.m_SceneValue.updateTime;
  114. data.type = "3";
  115. MsgHandler.AddListener(value.DownloadPath, HandleMsg);
  116. DownloadResManager.Instance.DownLoad(data);
  117. Debug.Log(" SetData " + updateTime);
  118. Data = data;
  119. }
  120. else
  121. {
  122. localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(value.DownloadPath);
  123. if (File.Exists(localSavePath))
  124. {
  125. // m_VideoCtr.VideoURL = Data.localSavePath;
  126. m_Video.SetUrl(localSavePath);
  127. }
  128. else
  129. {
  130. // m_VideoCtr.VideoURL = null;
  131. m_Video.SetUrl(null);
  132. }
  133. }
  134. }
  135. public void SetData(string url)
  136. {
  137. Debug.Log(url);
  138. localSavePath = Application.persistentDataPath + "/Material/" + Path.GetFileName(url);
  139. //if(m_VideoCtr ==null)
  140. // m_VideoCtr = transform.Find("Screen/Video").GetComponent<Navigator.VideoControl>();
  141. //m_VideoCtr.VideoURL = url;
  142. if (m_Video == null)
  143. m_Video = transform.Find("Screen/AVideo").GetComponent<AVProVideoPlayer>();
  144. m_Video.SetUrl(localSavePath);
  145. }
  146. private void HandleMsg(Msg msg)
  147. {
  148. Debug.Log("DGJ ===> TemplateVideo");
  149. if (File.Exists(localSavePath))
  150. {
  151. // m_VideoCtr.VideoURL = Data.localSavePath;
  152. m_Video.SetUrl(localSavePath);
  153. }
  154. else
  155. {
  156. // m_VideoCtr.VideoURL = null;
  157. m_Video.SetUrl(null);
  158. }
  159. }
  160. private void OnSyncVideo(SyncVideoData syncVideo)
  161. {
  162. if(syncVideo.spid == spid&& syncVideo.id == videoid)
  163. {
  164. if (syncVideo.isPlay)
  165. m_Video.Play();
  166. else
  167. m_Video.Pause();
  168. if (syncVideo.times != -1)
  169. m_Video.SetSeek(syncVideo.times);
  170. }
  171. }
  172. public override void HideCollider()
  173. {
  174. base.HideCollider();
  175. }
  176. }