Mp4Item.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Video;
  7. using static LangChaoRommMinIo;
  8. public class Mp4Item : BaseFilePrefabItem
  9. {
  10. public RawImage showVideo;
  11. public RectTransform rectTransform;
  12. // public VideoPlayer videoPlayer;
  13. public AVProVideoPlayer aVProVideoPlayer;
  14. public override void Init(FileConfig fileConfig)
  15. {
  16. hideBtn.onClick.AddListener(() => { StartCoroutine(CloseVideo()); });
  17. Debug.Log("Hjj "+ fileConfig.Url);
  18. base.Init(fileConfig);
  19. if (!string.IsNullOrEmpty(fileConfig.Url))
  20. {
  21. string url = fileConfig.Url;
  22. if (!fileConfig.Url.Contains("http"))
  23. {
  24. url = "https://" + fileConfig.Url;
  25. }
  26. // videoPlayer.url = url;
  27. }
  28. LangChaoRommMinIo.Instance.getFile(MQTTManager.Instance.roomId, fileConfig.Bucket, fileConfig.ObjectName, (RoomFileData rfd) => {
  29. Debug.Log("rfd==>" + rfd.url);
  30. Debug.Log("rfd==>" + rfd.bytes.Length);
  31. // url = rfd.url;
  32. // aVProVideoPlayer.SetUrl(url);
  33. StartCoroutine(SaveMp4(Application.persistentDataPath + "/" + fileConfig.UUid + Path.GetExtension(fileConfig.FileName), rfd.bytes));
  34. });
  35. }
  36. private IEnumerator CloseVideo()
  37. {
  38. transform.position += new Vector3(10000, 0, 0);
  39. yield return !string.IsNullOrEmpty(aVProVideoPlayer.GetUrl());
  40. if (RoomFile.Instance.FilePrefabConfigList.Contains(this.fileConfig))
  41. {
  42. RoomFile.Instance.FilePrefabConfigList.Remove(this.fileConfig);
  43. }
  44. this.gameObject.SetActive(false);
  45. if (this.fileConfig != null)
  46. {
  47. RoomFile.HidefileChooseAction?.Invoke(this.fileConfig);
  48. }
  49. }
  50. private IEnumerator SaveMp4(string path , byte[] bytes)
  51. {
  52. Debug.Log(" Path ==> "+ path);
  53. FileInfo file = new FileInfo(path);
  54. if (file.Exists)
  55. {
  56. // file.Delete();
  57. yield return new WaitForSeconds(0.02f);
  58. aVProVideoPlayer.SetUrl(path);
  59. }
  60. else
  61. {
  62. Stream sw = File.Create(path);
  63. // FileStream sw = new FileStream(path);
  64. sw.Write(bytes, 0, bytes.Length);
  65. sw.Close();
  66. sw.Dispose();
  67. yield return new WaitForSeconds(0.02f);
  68. aVProVideoPlayer.SetUrl(path);
  69. }
  70. }
  71. protected override void Start()
  72. {
  73. base.Start();
  74. StartCoroutine(enumerator());
  75. }
  76. private IEnumerator enumerator()
  77. {
  78. while (!aVProVideoPlayer.IsVideoReady())
  79. {
  80. yield return null;
  81. }
  82. float standard_width = 150f;
  83. float standard_height = 84f;
  84. //float video_width = videoPlayer.texture.width;
  85. //float video_height = videoPlayer.texture.height;
  86. float video_width = aVProVideoPlayer.GetVideoWidth();
  87. float video_height = aVProVideoPlayer.GetVideoHeight();
  88. if (standard_width < video_width && standard_height > video_height)
  89. {
  90. float video_aspect = standard_width / video_width;
  91. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  92. }
  93. else if (standard_width > video_width && standard_height < video_height)
  94. {
  95. float video_aspect = standard_height / video_height;
  96. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  97. }
  98. else if (standard_width > video_width && standard_height > video_height)
  99. {
  100. if (standard_width / video_width > standard_height / video_height)
  101. {
  102. float video_aspect = standard_height / video_height;
  103. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  104. }
  105. else
  106. {
  107. float video_aspect = standard_width / video_width;
  108. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  109. }
  110. }
  111. else
  112. {
  113. if (standard_width / video_width > standard_height / video_height)
  114. {
  115. float video_aspect = standard_height / video_height;
  116. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  117. }
  118. else
  119. {
  120. float video_aspect = standard_width / video_width;
  121. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  122. }
  123. }
  124. }
  125. }