TemplateVideo.cs 6.6 KB

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