using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Video; using System; using UnityEngine.UI; public class RemoteVideoControl : MonoBehaviour { public GameObject Icon; public GameObject Stop; bool? isStart = true; public VideoPlayer videoPlayer; public Slider slider; //当前时间 public Text currentTime; //影片时间 public Text textTotalTime; //是否获取总时长 private bool IsStart; //时 分的转换 private int hour, mint; private float time; public void VideoStartSwitch() { isStart = videoPlayer.isPlaying; isStart = !isStart; Icon.SetActive(!isStart.Value); Stop.SetActive(isStart.Value); if (isStart.Value) { videoPlayer?.Play(); } else { videoPlayer?.Pause(); } } private void Start() { IsStart = true; slider.onValueChanged.AddListener(delegate { ChangeVideo(slider.value); }); } public void ChangeVideo(float value) { if (videoPlayer.isPrepared) { float tempvalue = max * value; time = tempvalue; hour = (int)time / 60; mint = (int)time % 60; currentTime.text = string.Format("{0}:{1}", hour.ToString("00"), mint.ToString("00")); } if (value > 0.99) { Icon.SetActive(true); Stop.SetActive(false); } } float max; void Update() { if (videoPlayer.isPlaying && IsStart) { //帧数/帧速率=总时长 如果是本地直接赋值的视频,我们可以通过VideoClip.length获取总时长 max = videoPlayer.frameCount / videoPlayer.frameRate; time = max; hour = (int)time / 60; mint = (int)time % 60; // textTotalTime.text = string.Format("/ {0:D2}:{1:D2}", hour.ToString(), mint.ToString()); textTotalTime.text = string.Format("{0}:{1}", hour.ToString("00"), mint.ToString("00")); IsStart = !IsStart; } } }