using Agora.Rtc; using BeinLab.Util; using DG.Tweening; using LitJson; using ShadowStudio.Tool; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Video; using XRTool.Util; using XRTool.WorldUI; using RenderHeads.Media.AVProVideo; namespace ShadowStudio.Model { public class VideoComponent : UIComponent { private bool isMove; private Vector3 imageScale; private RenderTexture videoTexture; private XRVideoPlayer moviesPlayer; public float clickTime = 0.2f; public MeshRenderer mVideoRenderer; private float pressTime; public RenderTexture VideoTexture { get => videoTexture; set => videoTexture = value; } public XRVideoPlayer MoviesPlayer { get { if (!moviesPlayer) { //moviesPlayer = UnityUtil.GetBreadthChild(Dlg.FullUIRoot, "XRVideoPlayer"); moviesPlayer = GetComponent(); } return moviesPlayer; } } void Start() { StartCoroutine(PlayerFirstFrame()); // MoviesPlayer.applyToMesh.Player.MediaPath = } //public override void DestroyArt() //{ // MoviesPlayer.VideoPlayer.Stop(); // gameObject.SetActive(false); // base.DestroyArt(); //} /// /// 第一次打开加载时无缩略图,为生成缩略图 /// /// IEnumerator PlayerFirstFrame() { int temp = 0; yield return new WaitForSeconds(2f); while (temp < 2) { playVideoManager(); temp++; } } private void OnTransferSelect(bool isSelect) { if (!isSelect) { MoviesPlayer.hideControl(); } else { MoviesPlayer.showControl(); } } public override void SetContainer(ArtContainerHandler container, Object art, ArtInfo info) { base.SetContainer(container, art, info); if (art == null) { string targetUrl = info.Url; if (!targetUrl.StartsWith("http")) { targetUrl = ResourcesManager.ArtServerPath + "/" + targetUrl; } if (!targetUrl.Contains("https")) targetUrl = "https" + targetUrl.Substring(4, targetUrl.Length - 4); // MoviesPlayer.VideoPlayer.url = targetUrl; MoviesPlayer.AVProVideoPlayer.SetUrl(targetUrl); } else if (art is VideoClip) { Debug.Log(art.name); // MoviesPlayer.VideoPlayer.clip = art as VideoClip; MoviesPlayer.AVProVideoPlayer.SetUrl(Application.streamingAssetsPath + "/AVProVideoZYK/" + art.name + ".mp4"); } MoviesPlayer.XRSlider.onValueChanged.AddListener(setVidoPlayerTime); MoviesPlayer.XRSlider.OnSliderDown.AddListener(OnSliderDown); MoviesPlayer.XRSlider.OnSliderUp.AddListener(OnSliderUp); imageScale = MoviesPlayer.Pause.transform.localScale; if (container && container.Transfer) { container.Transfer.EditTransfer += OnEditTransfer; } this.GetComponent().offsetMax = Vector2.zero; this.GetComponent().offsetMin = Vector2.zero; } private void OnEditTransfer(TransferState state) { if (state == TransferState.Stop) { OnTransferSelect(true); } else if (state == TransferState.Over) { OnTransferSelect(false); } } void setVidoPlayerTime(float f) { } public void OnSliderDown(PointerEventData eventData) { MoviesPlayer.isDown = true; } public void OnSliderUp(PointerEventData eventData) { MoviesPlayer.sliderChanged(MoviesPlayer.XRSlider.value); StopCoroutine(pointerUp(false)); MoviesPlayer.isDown = true; StartCoroutine(pointerUp(true)); MoviesPlayer.isDown = true; } IEnumerator pointerUp(bool isSend = true) { yield return new WaitForSeconds(1f); MoviesPlayer.isDown = false; if (isSend) { VideoState("vdFrame", MoviesPlayer.XRSlider.value.ToString()); SendTransfer(0, (int)TransferState.Stop); //sendMessage(); } } public void playVideoManager() { isMove = Time.time - pressTime < clickTime; if (!isMove) { if (MoviesPlayer.AVProVideoPlayer.IsPlaying()) { PauseVideo(); } else { PlayVideo(); } } } public void PlayVideo(bool isSend = true) { if (!MoviesPlayer.AVProVideoPlayer.IsPlaying()) { MoviesPlayer.Pause.transform.DOScale(imageScale * 1.1f, 0.3f).OnComplete(() => { MoviesPlayer.Pause.transform.localScale = Vector3.zero; }).OnKill(() => { MoviesPlayer.Pause.transform.localScale = Vector3.zero; }); // MoviesPlayer.VideoPlayer.Play(); MoviesPlayer.AVProVideoPlayer.Play(); VideoState("vdFrame", MoviesPlayer.XRSlider.value.ToString()); MoviesPlayer.hideControl(); } if (isSend) { VideoState("vdPlay", "1"); //sendMessage(); SendTransfer(0, (int)TransferState.Stop); } } public void PauseVideo(bool isSend = true) { if (MoviesPlayer.AVProVideoPlayer.IsPlaying()) { MoviesPlayer.Pause.transform.localScale = imageScale * 1.1f; MoviesPlayer.Pause.transform.DOScale(imageScale, 0.3f).OnComplete(() => { MoviesPlayer.Pause.transform.localScale = imageScale; }).OnKill(() => { MoviesPlayer.Pause.transform.localScale = imageScale; }); // MoviesPlayer.VideoPlayer.Pause(); MoviesPlayer.AVProVideoPlayer.Pause(); VideoState("vdFrame", MoviesPlayer.XRSlider.value.ToString()); } if (isSend) { VideoState("vdPlay", "0"); //sendMessage(); SendTransfer(0, (int)TransferState.Stop); } } //public override void SendTransfer(int state, bool isEditting) //{ // base.SendTransfer(state, isEditting); //} bool isPlay; public override void TransferSyn(GoodsInfo goods, bool smooth = true) { base.TransferSyn(goods, smooth); // Debug.Log(goods.goods_extended); CheckVideoState(goods.goods_extended); // goods.goods_extended } private void CheckVideoState(string goods_extended) { try { JsonData dataInfo = JsonMapper.ToObject(goods_extended); if (dataInfo != null) { if (dataInfo.Keys.Contains("vdPlay")) { if (dataInfo["vdPlay"].ToString() == "1") { isPlay = true; PlayVideo(false); } else { isPlay = false; PauseVideo(false); } } if (dataInfo.Keys.Contains("vdFrame")) { if (Mathf.Abs(MoviesPlayer.XRSlider.value - float.Parse(dataInfo["vdFrame"].ToString())) > 0.01f) { StartCoroutine(pointerUp(false)); MoviesPlayer.isDown = true; MoviesPlayer.XRSlider.value = float.Parse(dataInfo["vdFrame"].ToString()); MoviesPlayer.sliderChanged(MoviesPlayer.XRSlider.value); } } } } catch { } } void VideoState(string type, string info) { JsonData dataInfo = new JsonData(); try { dataInfo = JsonMapper.ToObject(Container.data.goods_extended); } catch { } dataInfo[type] = info; Container.data.goods_extended = dataInfo.ToJson(); } } }