暂停播放+进度条控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
public class VedioPlayPanel : MonoBehaviour
{
public VideoPlayer videoPlayer;
public Slider _videoSeekSlider;
public Image vedioPauseImg;
public Toggle vedioToggle;
public Text titleText, nowTime, vedioTotalTime;
private bool isPausing, isDrag = false;
private void Start()
{
vedioToggle.onValueChanged.AddListener(PlayAndPause);
_videoSeekSlider.onValueChanged.AddListener((value) => { ChangeVideoFrame(); });
videoPlayer.loopPointReached += (VideoPlayer source) =>
{
videoPlayer.frame = 0;
vedioToggle.isOn = false;
};
}
private void FixedUpdate()
{
nowTime.text = GetFormatTimeText(videoPlayer.time);
if (!isDrag)
{
_videoSeekSlider.value = (float.Parse(videoPlayer.frame.ToString()) / float.Parse(videoPlayer.frameCount.ToString()));
}
}
public void PlayVedioOnInit(string vedioPath)
{
videoPlayer.targetTexture.Release();
titleText.text = vedioPath.Split("/")[1].Substring(0, 2);
videoPlayer.url = Application.streamingAssetsPath + "/" + vedioPath;
videoPlayer.Play();
videoPlayer.audioOutputMode = VideoAudioOutputMode.Direct;
Invoke("InitTotalText", 0.5f);
switch (titleText.text)
{
case "规划":
StartCoroutine(NetWorkInterfaceManager.SendUserCtrMsg(1));
break;
case "建设":
StartCoroutine(NetWorkInterfaceManager.SendUserCtrMsg(2));
break;
case "运维":
StartCoroutine(NetWorkInterfaceManager.SendUserCtrMsg(3));
break;
default:
break;
}
}
private void InitTotalText()
{
vedioTotalTime.text = GetFormatTimeText(videoPlayer.length);
}
public void PlayAndPause(bool ison)
{
isPausing = !ison;
vedioPauseImg.gameObject.SetActive(!ison);
if (ison)
{
videoPlayer.Play();
}
else
{
videoPlayer.Pause();
}
}
public void ChangeVideoFrame()
{
if (isDrag)
{
videoPlayer.frame = long.Parse((_videoSeekSlider.value * videoPlayer.frameCount).ToString("0."));
}
}
public void OnSliderDown()
{
isDrag = true;
videoPlayer.Pause();
ChangeVideoFrame();
}
public void OnSliderUp()
{
isDrag = false;
if (!isPausing)
{
videoPlayer.Play();
}
}
public void ClosePanel()
{
videoPlayer.Stop();
gameObject.SetActive(false);
}
private string GetFormatTimeText(double second)
{
return string.Format("{0:D2}:{1:D2}", (int)second / 60, (int)second % 60);
}
}