Mp4Item.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Video;
  6. public class Mp4Item : BaseFilePrefabItem
  7. {
  8. public RawImage showVideo;
  9. public RectTransform rectTransform;
  10. // public VideoPlayer videoPlayer;
  11. public AVProVideoPlayer aVProVideoPlayer;
  12. public override void Init(FileConfig fileConfig)
  13. {
  14. Debug.Log("Hjj "+ fileConfig.Url);
  15. base.Init(fileConfig);
  16. if (!string.IsNullOrEmpty(fileConfig.Url))
  17. {
  18. string url = fileConfig.Url;
  19. if (!fileConfig.Url.Contains("http"))
  20. {
  21. url = "https://" + fileConfig.Url;
  22. }
  23. // videoPlayer.url = url;
  24. aVProVideoPlayer.SetUrl(url);
  25. }
  26. }
  27. protected override void Start()
  28. {
  29. base.Start();
  30. StartCoroutine(enumerator());
  31. }
  32. private IEnumerator enumerator()
  33. {
  34. while (!aVProVideoPlayer.IsVideoReady())
  35. {
  36. yield return null;
  37. }
  38. float standard_width = 150f;
  39. float standard_height = 84f;
  40. //float video_width = videoPlayer.texture.width;
  41. //float video_height = videoPlayer.texture.height;
  42. float video_width = aVProVideoPlayer.GetVideoWidth();
  43. float video_height = aVProVideoPlayer.GetVideoHeight();
  44. if (standard_width < video_width && standard_height > video_height)
  45. {
  46. float video_aspect = standard_width / video_width;
  47. rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
  48. }
  49. else if (standard_width > video_width && standard_height < video_height)
  50. {
  51. float video_aspect = standard_height / video_height;
  52. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
  53. }
  54. else if (standard_width > video_width && standard_height > video_height)
  55. {
  56. if (standard_width / video_width > standard_height / video_height)
  57. {
  58. float video_aspect = standard_height / video_height;
  59. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  60. }
  61. else
  62. {
  63. float video_aspect = standard_width / video_width;
  64. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  65. }
  66. }
  67. else
  68. {
  69. if (standard_width / video_width > standard_height / video_height)
  70. {
  71. float video_aspect = standard_height / video_height;
  72. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  73. }
  74. else
  75. {
  76. float video_aspect = standard_width / video_width;
  77. rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
  78. }
  79. }
  80. }
  81. }