using System; using System.Collections; using System.Collections.Generic; using Kill.Core; using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; namespace Kill.UI.Components { public class VideoCtrl : MonoBehaviour { public bool istest; public VideoPlayer videoPlayer; public bool needFullScreen = false; bool isFullScreen = false; /// /// 0:正常播放 1:全屏播放 /// public List renderTextures; public RawImage windowImage; public RawImage fullImage; public GameObject playButton; public GameObject pauseButton; public Text totalTimeText; public Text nowTime; public Slider slider; bool isplaying = false; bool ispause = false; Action playOver; string url; private void FixedUpdate() { if (isplaying) { OnVideoPlaying(); } } public void Init(string url, Action playOver) { this.playOver = playOver; this.url = url; SetVideo(url); if (playButton != null) { playButton.SetActive(false); } if (pauseButton != null) { pauseButton.SetActive(false); } ispause = true; } public void PlayVideo() { StartCoroutine(YieldPlay()); } public IEnumerator YieldPlay() { yield return new WaitForSeconds(0.1f); ispause = false; videoPlayer.Play(); if (playButton != null) { playButton.SetActive(false); } if (pauseButton != null) { pauseButton.SetActive(true); } isplaying = true; } public void PauseVideo() { ispause = true; videoPlayer.Pause(); isplaying = false; if (playButton != null) { playButton.SetActive(true); } if (pauseButton != null) { pauseButton.SetActive(false); } } public void RePlay() { videoPlayer.Stop(); videoPlayer.frame = 0; PlayVideo(); } public void SetVideo(string url) { videoPlayer.url = url; videoPlayer.prepareCompleted += VideoPrepared; videoPlayer.Prepare(); if (playButton != null) { playButton.SetActive(true); } } int totalTime = 0; public void VideoPrepared(VideoPlayer player) { if (renderTextures != null && renderTextures.Count > 0) { foreach (var r in renderTextures) Destroy(r); renderTextures.Clear(); } renderTextures=new List(); RenderTexture texture = new RenderTexture((int)player.width, (int)player.height, 1); renderTextures.Add(texture); player.targetTexture = texture; windowImage.texture = texture; if (needFullScreen) { var size = fullImage.GetComponentInParent().rect; RenderTexture fullTexture = new RenderTexture((int)size.height, (int)size.width, 1); fullImage.GetComponent().sizeDelta = new Vector2(size.height, size.width); renderTextures.Add(fullTexture); fullImage.texture = fullTexture; } videoPlayer.frame = 0; if (slider != null) { totalTime = (int)(player.frameCount / player.frameRate + 0.5f); totalTimeText.text = SecondToHour(totalTime); } videoPlayer.prepareCompleted -= VideoPrepared; player.loopPointReached += VideoOver; if (playButton != null) playButton.SetActive(true); } public void ShowAnimation(int dir) { Animation Anim = GetComponent(); if (Anim != null) { if (dir == -1) { Anim["视频出现"].time = Anim["视频出现"].clip.length; Anim["视频出现"].speed = -1; Anim.Play("视频出现"); } else { Anim["视频出现"].time = 0; Anim["视频出现"].speed = 1; Anim.Play("视频出现"); } } } long lastFrame = 0; /// /// 播放中事件(给进度条赋值) /// public void OnVideoPlaying() { if (slider == null) return; if (Mathf.Abs(videoPlayer.frame - lastFrame) < 1) return; slider.value = (videoPlayer.frame + 1) / (float)videoPlayer.frameCount; lastFrame = videoPlayer.frame; nowTime.text = SecondToHour((int)(totalTime * slider.value)); } /// /// 拖动进度条 /// public void PointDownUp() { //if (slider.value != lastValue) //{ // videoPlayer.frame = (long)(videoPlayer.frameCount * slider.value); //} if (slider.value >= 0.98f) { videoPlayer.Stop(); VideoOver(videoPlayer); return; } videoPlayer.frame = (long)(videoPlayer.frameCount * slider.value) - 1; nowTime.text = SecondToHour((int)(totalTime * slider.value)); if (!ispause) { videoPlayer.Play(); isplaying = true; } // TeacherServer.m_instance.SendVideoProcess((long)(videoPlayer.frameCount * slider.value) - 1); } public void PointDown() { videoPlayer.Pause(); isplaying = false; } public void Draging() { videoPlayer.frame = (long)(videoPlayer.frameCount * slider.value) - 1; nowTime.text = SecondToHour((int)(totalTime * slider.value)); } public void VideoOver(VideoPlayer vp) { vp.loopPointReached -= VideoOver; isplaying = false; if (playButton != null) { playButton.SetActive(true); } if (pauseButton != null) { pauseButton.SetActive(false); } playOver(); Init(url, playOver); } public void SetFullScreen(bool isFullScreen) { this.isFullScreen = isFullScreen; if (isFullScreen) { #if UNITY_ANDROID AndroidStatusBar.statusBarState = AndroidStatusBar.States.Hidden; #endif videoPlayer.targetTexture = renderTextures[1]; fullImage.transform.parent.gameObject.SetActive(true); UIManager.Instance.AddBackAction(() => SetFullScreen(false)); if (ispause) PlayVideo(); } else { #if UNITY_ANDROID AndroidStatusBar.statusBarState = AndroidStatusBar.States.TranslucentOverContent; #endif videoPlayer.targetTexture = renderTextures[0]; fullImage.transform.parent.gameObject.SetActive(false); UIManager.Instance.RecoverBackAction(); } } /// /// 秒转换小时 /// /// /// public string SecondToHour(float time) { int hour = 0; int minute = 0; int second = 0; second = (int)time; if (second > 60) { minute = second / 60; second = second % 60; } if (minute > 60) { hour = minute / 60; minute = minute % 60; } string result = ""; if (hour > 0) { result += hour + ":"; } if (minute < 10) result += "0"; result += minute + ":"; if (second < 10) result += "0"; result += second.ToString(); return result; } } }