123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Video;
- using static LangChaoRommMinIo;
- public class Mp4Item : BaseFilePrefabItem
- {
- public RawImage showVideo;
- public RectTransform rectTransform;
- // public VideoPlayer videoPlayer;
- public AVProVideoPlayer aVProVideoPlayer;
- public override void Init(FileConfig fileConfig)
- {
- hideBtn.onClick.AddListener(() => { StartCoroutine(CloseVideo()); });
- Debug.Log("Hjj "+ fileConfig.Url);
- base.Init(fileConfig);
- if (!string.IsNullOrEmpty(fileConfig.Url))
- {
- string url = fileConfig.Url;
- if (!fileConfig.Url.Contains("http"))
- {
- url = "https://" + fileConfig.Url;
- }
-
- // videoPlayer.url = url;
-
- }
- LangChaoRommMinIo.Instance.getFile(MQTTManager.Instance.roomId, fileConfig.Bucket, fileConfig.ObjectName, (RoomFileData rfd) => {
- Debug.Log("rfd==>" + rfd.url);
- Debug.Log("rfd==>" + rfd.bytes.Length);
- // url = rfd.url;
- // aVProVideoPlayer.SetUrl(url);
- StartCoroutine(SaveMp4(Application.persistentDataPath + "/" + fileConfig.UUid + Path.GetExtension(fileConfig.FileName), rfd.bytes));
- });
- }
- private IEnumerator CloseVideo()
- {
- transform.position += new Vector3(10000, 0, 0);
- yield return !string.IsNullOrEmpty(aVProVideoPlayer.GetUrl());
- if (RoomFile.Instance.FilePrefabConfigList.Contains(this.fileConfig))
- {
- RoomFile.Instance.FilePrefabConfigList.Remove(this.fileConfig);
- }
- this.gameObject.SetActive(false);
- if (this.fileConfig != null)
- {
- RoomFile.HidefileChooseAction?.Invoke(this.fileConfig);
- }
- }
- private IEnumerator SaveMp4(string path , byte[] bytes)
- {
- Debug.Log(" Path ==> "+ path);
- FileInfo file = new FileInfo(path);
- if (file.Exists)
- {
- // file.Delete();
- yield return new WaitForSeconds(0.02f);
- aVProVideoPlayer.SetUrl(path);
- }
- else
- {
- Stream sw = File.Create(path);
- // FileStream sw = new FileStream(path);
- sw.Write(bytes, 0, bytes.Length);
- sw.Close();
- sw.Dispose();
- yield return new WaitForSeconds(0.02f);
- aVProVideoPlayer.SetUrl(path);
- }
-
- }
- protected override void Start()
- {
- base.Start();
- StartCoroutine(enumerator());
- }
- private IEnumerator enumerator()
- {
- while (!aVProVideoPlayer.IsVideoReady())
- {
- yield return null;
- }
- float standard_width = 150f;
- float standard_height = 84f;
- //float video_width = videoPlayer.texture.width;
- //float video_height = videoPlayer.texture.height;
- float video_width = aVProVideoPlayer.GetVideoWidth();
- float video_height = aVProVideoPlayer.GetVideoHeight();
- if (standard_width < video_width && standard_height > video_height)
- {
- float video_aspect = standard_width / video_width;
- rectTransform.sizeDelta = new Vector2(standard_width, video_height * video_aspect);
- }
- else if (standard_width > video_width && standard_height < video_height)
- {
- float video_aspect = standard_height / video_height;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, standard_height);
- }
- else if (standard_width > video_width && standard_height > video_height)
- {
- if (standard_width / video_width > standard_height / video_height)
- {
- float video_aspect = standard_height / video_height;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- else
- {
- float video_aspect = standard_width / video_width;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- }
- else
- {
- if (standard_width / video_width > standard_height / video_height)
- {
- float video_aspect = standard_height / video_height;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- else
- {
- float video_aspect = standard_width / video_width;
- rectTransform.sizeDelta = new Vector2(video_width * video_aspect, video_height * video_aspect);
- }
- }
- }
- }
|