VideoComponent.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using Agora.Rtc;
  2. using BeinLab.Util;
  3. using DG.Tweening;
  4. using LitJson;
  5. using ShadowStudio.Tool;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.EventSystems;
  10. using UnityEngine.Video;
  11. using XRTool.Util;
  12. using XRTool.WorldUI;
  13. using RenderHeads.Media.AVProVideo;
  14. namespace ShadowStudio.Model
  15. {
  16. public class VideoComponent : UIComponent
  17. {
  18. private bool isMove;
  19. private Vector3 imageScale;
  20. private RenderTexture videoTexture;
  21. private XRVideoPlayer moviesPlayer;
  22. public float clickTime = 0.2f;
  23. public MeshRenderer mVideoRenderer;
  24. private float pressTime;
  25. public RenderTexture VideoTexture { get => videoTexture; set => videoTexture = value; }
  26. public XRVideoPlayer MoviesPlayer
  27. {
  28. get
  29. {
  30. if (!moviesPlayer)
  31. {
  32. //moviesPlayer = UnityUtil.GetBreadthChild<XRVideoPlayer>(Dlg.FullUIRoot, "XRVideoPlayer");
  33. moviesPlayer = GetComponent<XRVideoPlayer>();
  34. }
  35. return moviesPlayer;
  36. }
  37. }
  38. void Start()
  39. {
  40. StartCoroutine(PlayerFirstFrame());
  41. // MoviesPlayer.applyToMesh.Player.MediaPath =
  42. }
  43. //public override void DestroyArt()
  44. //{
  45. // MoviesPlayer.VideoPlayer.Stop();
  46. // gameObject.SetActive(false);
  47. // base.DestroyArt();
  48. //}
  49. /// <summary>
  50. /// 第一次打开加载时无缩略图,为生成缩略图
  51. /// </summary>
  52. /// <returns></returns>
  53. IEnumerator PlayerFirstFrame()
  54. {
  55. int temp = 0;
  56. yield return new WaitForSeconds(2f);
  57. while (temp < 2)
  58. {
  59. playVideoManager();
  60. temp++;
  61. }
  62. }
  63. private void OnTransferSelect(bool isSelect)
  64. {
  65. if (!isSelect)
  66. {
  67. MoviesPlayer.hideControl();
  68. }
  69. else
  70. {
  71. MoviesPlayer.showControl();
  72. }
  73. }
  74. public override void SetContainer(ArtContainerHandler container, Object art, ArtInfo info)
  75. {
  76. base.SetContainer(container, art, info);
  77. if (art == null)
  78. {
  79. string targetUrl = info.Url;
  80. if (!targetUrl.StartsWith("http"))
  81. {
  82. targetUrl = ResourcesManager.ArtServerPath + "/" + targetUrl;
  83. }
  84. if (!targetUrl.Contains("https"))
  85. targetUrl = "https" + targetUrl.Substring(4, targetUrl.Length - 4);
  86. // MoviesPlayer.VideoPlayer.url = targetUrl;
  87. MoviesPlayer.AVProVideoPlayer.SetUrl(targetUrl);
  88. }
  89. else if (art is VideoClip)
  90. {
  91. Debug.Log(art.name);
  92. // MoviesPlayer.VideoPlayer.clip = art as VideoClip;
  93. MoviesPlayer.AVProVideoPlayer.SetUrl(Application.streamingAssetsPath + "/AVProVideoZYK/" + art.name + ".mp4");
  94. }
  95. MoviesPlayer.XRSlider.onValueChanged.AddListener(setVidoPlayerTime);
  96. MoviesPlayer.XRSlider.OnSliderDown.AddListener(OnSliderDown);
  97. MoviesPlayer.XRSlider.OnSliderUp.AddListener(OnSliderUp);
  98. imageScale = MoviesPlayer.Pause.transform.localScale;
  99. if (container && container.Transfer)
  100. {
  101. container.Transfer.EditTransfer += OnEditTransfer;
  102. }
  103. this.GetComponent<RectTransform>().offsetMax = Vector2.zero;
  104. this.GetComponent<RectTransform>().offsetMin = Vector2.zero;
  105. }
  106. private void OnEditTransfer(TransferState state)
  107. {
  108. if (state == TransferState.Stop)
  109. {
  110. OnTransferSelect(true);
  111. }
  112. else if (state == TransferState.Over)
  113. {
  114. OnTransferSelect(false);
  115. }
  116. }
  117. void setVidoPlayerTime(float f)
  118. {
  119. }
  120. public void OnSliderDown(PointerEventData eventData)
  121. {
  122. MoviesPlayer.isDown = true;
  123. }
  124. public void OnSliderUp(PointerEventData eventData)
  125. {
  126. MoviesPlayer.sliderChanged(MoviesPlayer.XRSlider.value);
  127. StopCoroutine(pointerUp(false));
  128. MoviesPlayer.isDown = true;
  129. StartCoroutine(pointerUp(true));
  130. MoviesPlayer.isDown = true;
  131. }
  132. IEnumerator pointerUp(bool isSend = true)
  133. {
  134. yield return new WaitForSeconds(1f);
  135. MoviesPlayer.isDown = false;
  136. if (isSend)
  137. {
  138. VideoState("vdFrame", MoviesPlayer.XRSlider.value.ToString());
  139. SendTransfer(0, (int)TransferState.Stop);
  140. //sendMessage();
  141. }
  142. }
  143. public void playVideoManager()
  144. {
  145. isMove = Time.time - pressTime < clickTime;
  146. if (!isMove)
  147. {
  148. if (MoviesPlayer.AVProVideoPlayer.IsPlaying())
  149. {
  150. PauseVideo();
  151. }
  152. else
  153. {
  154. PlayVideo();
  155. }
  156. }
  157. }
  158. public void PlayVideo(bool isSend = true)
  159. {
  160. if (!MoviesPlayer.AVProVideoPlayer.IsPlaying())
  161. {
  162. MoviesPlayer.Pause.transform.DOScale(imageScale * 1.1f, 0.3f).OnComplete<Tween>(() =>
  163. {
  164. MoviesPlayer.Pause.transform.localScale = Vector3.zero;
  165. }).OnKill<Tween>(() =>
  166. {
  167. MoviesPlayer.Pause.transform.localScale = Vector3.zero;
  168. });
  169. // MoviesPlayer.VideoPlayer.Play();
  170. MoviesPlayer.AVProVideoPlayer.Play();
  171. VideoState("vdFrame", MoviesPlayer.XRSlider.value.ToString());
  172. MoviesPlayer.hideControl();
  173. }
  174. if (isSend)
  175. {
  176. VideoState("vdPlay", "1");
  177. //sendMessage();
  178. SendTransfer(0, (int)TransferState.Stop);
  179. }
  180. }
  181. public void PauseVideo(bool isSend = true)
  182. {
  183. if (MoviesPlayer.AVProVideoPlayer.IsPlaying())
  184. {
  185. MoviesPlayer.Pause.transform.localScale = imageScale * 1.1f;
  186. MoviesPlayer.Pause.transform.DOScale(imageScale, 0.3f).OnComplete<Tween>(() =>
  187. {
  188. MoviesPlayer.Pause.transform.localScale = imageScale;
  189. }).OnKill<Tween>(() =>
  190. {
  191. MoviesPlayer.Pause.transform.localScale = imageScale;
  192. });
  193. // MoviesPlayer.VideoPlayer.Pause();
  194. MoviesPlayer.AVProVideoPlayer.Pause();
  195. VideoState("vdFrame", MoviesPlayer.XRSlider.value.ToString());
  196. }
  197. if (isSend)
  198. {
  199. VideoState("vdPlay", "0");
  200. //sendMessage();
  201. SendTransfer(0, (int)TransferState.Stop);
  202. }
  203. }
  204. //public override void SendTransfer(int state, bool isEditting)
  205. //{
  206. // base.SendTransfer(state, isEditting);
  207. //}
  208. bool isPlay;
  209. public override void TransferSyn(GoodsInfo goods, bool smooth = true)
  210. {
  211. base.TransferSyn(goods, smooth);
  212. // Debug.Log(goods.goods_extended);
  213. CheckVideoState(goods.goods_extended);
  214. // goods.goods_extended
  215. }
  216. private void CheckVideoState(string goods_extended)
  217. {
  218. try
  219. {
  220. JsonData dataInfo = JsonMapper.ToObject(goods_extended);
  221. if (dataInfo != null)
  222. {
  223. if (dataInfo.Keys.Contains("vdPlay"))
  224. {
  225. if (dataInfo["vdPlay"].ToString() == "1")
  226. {
  227. isPlay = true;
  228. PlayVideo(false);
  229. }
  230. else
  231. {
  232. isPlay = false;
  233. PauseVideo(false);
  234. }
  235. }
  236. if (dataInfo.Keys.Contains("vdFrame"))
  237. {
  238. if (Mathf.Abs(MoviesPlayer.XRSlider.value - float.Parse(dataInfo["vdFrame"].ToString())) > 0.01f)
  239. {
  240. StartCoroutine(pointerUp(false));
  241. MoviesPlayer.isDown = true;
  242. MoviesPlayer.XRSlider.value = float.Parse(dataInfo["vdFrame"].ToString());
  243. MoviesPlayer.sliderChanged(MoviesPlayer.XRSlider.value);
  244. }
  245. }
  246. }
  247. }
  248. catch
  249. {
  250. }
  251. }
  252. void VideoState(string type, string info)
  253. {
  254. JsonData dataInfo = new JsonData();
  255. try
  256. {
  257. dataInfo = JsonMapper.ToObject(Container.data.goods_extended);
  258. }
  259. catch
  260. {
  261. }
  262. dataInfo[type] = info;
  263. Container.data.goods_extended = dataInfo.ToJson();
  264. }
  265. }
  266. }