Mp4Item.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. Debug.Log("Hjj "+ fileConfig.Url);
  17. base.Init(fileConfig);
  18. if (!string.IsNullOrEmpty(fileConfig.Url))
  19. {
  20. string url = fileConfig.Url;
  21. if (!fileConfig.Url.Contains("http"))
  22. {
  23. url = "https://" + fileConfig.Url;
  24. }
  25. LangChaoRommMinIo.Instance.getFile(MQTTManager.Instance.roomId, fileConfig.Bucket, fileConfig.ObjectName, (RoomFileData rfd) => {
  26. Debug.Log("rfd==>" + rfd.url);
  27. Debug.Log("rfd==>" + rfd.bytes.Length);
  28. // url = rfd.url;
  29. // aVProVideoPlayer.SetUrl(url);
  30. StartCoroutine(SaveMp4(Application.persistentDataPath + "/" + fileConfig.FileName, rfd.bytes));
  31. });
  32. // videoPlayer.url = url;
  33. }
  34. }
  35. private IEnumerator SaveMp4(string path , byte[] bytes)
  36. {
  37. Debug.Log(" Path ==> "+ path);
  38. FileInfo file = new FileInfo(path);
  39. if (file.Exists)
  40. {
  41. file.Delete();
  42. }
  43. Stream sw = File.Create(path);
  44. // FileStream sw = new FileStream(path);
  45. sw.Write(bytes, 0, bytes.Length);
  46. sw.Close();
  47. sw.Dispose();
  48. yield return new WaitForSeconds(0.02f);
  49. aVProVideoPlayer.SetUrl(path);
  50. }
  51. protected override void Start()
  52. {
  53. base.Start();
  54. StartCoroutine(enumerator());
  55. }
  56. private IEnumerator enumerator()
  57. {
  58. while (!aVProVideoPlayer.IsVideoReady())
  59. {
  60. yield return null;
  61. }
  62. float standard_width = 150f;
  63. float standard_height = 84f;
  64. //float video_width = videoPlayer.texture.width;
  65. //float video_height = videoPlayer.texture.height;
  66. float video_width = aVProVideoPlayer.GetVideoWidth();
  67. float video_height = aVProVideoPlayer.GetVideoHeight();
  68. if (standard_width < video_width && standard_height > video_height)
  69. {
  70. float video_aspect = standard_width / video_width;
  71. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  72. }
  73. else if (standard_width > video_width && standard_height < video_height)
  74. {
  75. float video_aspect = standard_height / video_height;
  76. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  77. }
  78. else if (standard_width > video_width && standard_height > video_height)
  79. {
  80. if (standard_width / video_width > standard_height / video_height)
  81. {
  82. float video_aspect = standard_height / video_height;
  83. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  84. }
  85. else
  86. {
  87. float video_aspect = standard_width / video_width;
  88. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  89. }
  90. }
  91. else
  92. {
  93. 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, video_height * video_aspect);
  97. }
  98. else
  99. {
  100. float video_aspect = standard_width / video_width;
  101. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  102. }
  103. }
  104. }
  105. }