using BigSpace.XRCore.RunningScene; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Playables; using UnityEngine.Video; namespace BigSpace.XRCore.Video { public class VideoPlayControll : MonoBehaviour { public string videoName; VideoPlayer video; private bool playOnAwake = false; public VideoData videoData = new VideoData(); float recoveryTime = 0; // Start is called before the first frame update void Awake() { video = this.gameObject.GetComponent(); video.url = VideoPathUtil.GetVideoUrlPath(videoName); videoData.name = videoName; playOnAwake = video.playOnAwake; video.playOnAwake = false; video.Prepare(); video.prepareCompleted += OnPrepared; RunningSceneMgr.Instance.videoCtr = this; } void OnPrepared(VideoPlayer vp) { Debug.Log("加载视频好了"); //if (RunningSceneMgr.Instance.RecoverData == null) //无断线数据 //{ // Debug.Log("没有离线视频数据"); vp.Play(); //} //else //{ // Debug.Log("有离线视频数据"); // StartCoroutine(WaitPlay(vp)); //} } IEnumerator WaitPlay(VideoPlayer vp) { yield return new WaitForSeconds(0.5f); //有离线数据 Play(recoveryTime); recoveryTime = 0; } void Update() { if (video.isPlaying) { videoData.time = (float)video.time; //Debug.Log("视频时间:"+ video.time); } } public void NormalPlay() { if (playOnAwake) Play(); } public void RecoveryPlay(float time) { Debug.Log("有离线视频进度:" + time); recoveryTime = time; //这里视频可能没有加载完成,所以要等视频加载完才能播放 } private void Play(float time = 0f) { //Debug.Log("播放video走这里了:" + time + "||" + transform.parent.name + "||" + transform.parent.parent.name); if (time != 0) //0不需要重新播放会闪两次 { Debug.Log("传来:" + time + " 视频最长多少:" + video.length); if (time >= video.length) { time = (float)(video.length - 1); } video.time = time; video.Play(); } } public void Stop() { if (video != null) video.Stop(); } public void ChangePlaySpeed(bool isSpeedUp) { if (video.isPlaying) video.playbackSpeed = isSpeedUp ? 8 : 1; } public IEnumerator IsAllPrepared() { while (true) { var isRet = true; if (!video.isPrepared) { isRet = false; break; } if (!isRet) yield return null; else yield break; } } private void OnDestroy() { video.clip = null; video.prepareCompleted -= OnPrepared; if (RunningSceneMgr.Instance != null) RunningSceneMgr.Instance.videoCtr = null; } } }