using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mx.UI;
using Mx.Utils;
using UnityEngine.UI;
using UnityEngine.Video;
///
public class VideoUIForm : BaseUIForm
{
private ImageAdaptive imageAdaptive;
private RawImage rawImage;
private VideoPlayer videoPlayer;
private bool isPlaying = false;
private Slider slider;
private Text playTimeText;
private Text lengthText;
private GameObject playButtonGo;
private GameObject playIconGo;
private GameObject pauseIconGo;
private bool sliderIsDrag = false;
private float playTime;
private float length;
public override void OnAwake()
{
base.OnAwake();
init();
rigisterButtonEvent();
}
private void init()
{
slider = UnityHelper.FindTheChildNode(gameObject, "Slider").GetComponent
rawImage = UnityHelper.FindTheChildNode(gameObject, "Stage").GetComponent
rawImage.enabled = false;
videoPlayer = rawImage.gameObject.GetComponent
if (videoPlayer == null) videoPlayer = rawImage.gameObject.AddComponent
playTimeText = UnityHelper.FindTheChildNode(gameObject, "PlayTime").GetComponent
lengthText = UnityHelper.FindTheChildNode(gameObject, "Length").GetComponent
playButtonGo = UnityHelper.FindTheChildNode(gameObject, "BtnPlay").gameObject;
playIconGo = UnityHelper.FindTheChildNode(gameObject, "PlayIcon").gameObject;
pauseIconGo = UnityHelper.FindTheChildNode(gameObject, "PauseIcon").gameObject;
imageAdaptive = rawImage.GetComponent
if (imageAdaptive == null) imageAdaptive = rawImage.gameObject.AddComponent
videoPlayer.playOnAwake = false; // 设置不自动播放
videoPlayer.waitForFirstFrame = true; // 等待第一帧加载完成再显示
videoPlayer.source = VideoSource.Url;
videoPlayer.aspectRatio = VideoAspectRatio.FitVertically;
videoPlayer.prepareCompleted += onVideoPrepared; // 视频加载完成事件
videoPlayer.loopPointReached += onVideoEnd; // 视频播放完成事件
playIconGo.SetActive(true);
pauseIconGo.SetActive(false);
initFinish = true;
}
void Update()
{
if (!sliderIsDrag)
{
float progress = (float)videoPlayer.time / (float)videoPlayer.length;
if (progress > 0) slider.value = progress;
length = (float)videoPlayer.length;
lengthText.text = formatTime(length);
}
playTime = (float)videoPlayer.time;
playTimeText.text = formatTime(playTime);
playButtonGo.SetActive(!isPlaying);
playIconGo.SetActive(!isPlaying);
pauseIconGo.SetActive(isPlaying);
}
///
public override void OnCloseUIEvent()
{
base.OnCloseUIEvent();
clearData();
}
public override void OnUIMsgEvent(string key, object values)
{
base.OnUIMsgEvent(key, values);
switch (key)
{
case MsgDefine.UPDATE_PANEL_DATA:
if (values != null)
{
if (initFinish)
{
rawImage.enabled = false;
string path = (string)values;
string encodedUrl = System.Uri.EscapeUriString(path);
videoPlayer.url = encodedUrl;
videoPlayer.Play();
isPlaying = true;
}
}
break;
}
}
///
private void rigisterButtonEvent()
{
RigisterButtonEvent("BtnClose", (clickObj) => { CloseCurrentUIForm(); });
RigisterButtonEvent("BtnPlayAndPause", (clickObj) => { PlayPauseVideo(); });
RigisterButtonEvent("BtnPlay", (clickObj) =>
{
videoPlayer.Play();
isPlaying = true;
});
}
///
public void OnSliderDragChange(bool isDrag)
{
sliderIsDrag = isDrag;
}
///
private void clearData()
{
if (!initFinish) return;
isPlaying = false;
slider.value = 0;
playTimeText.text = "00:00";
lengthText.text = "00:00";
playButtonGo.SetActive(true);
playIconGo.SetActive(true);
pauseIconGo.SetActive(false);
sliderIsDrag = false;
}
///
private void onVideoPrepared(VideoPlayer source)
{
rawImage.texture = source.texture;
source.Play();
rawImage.enabled = true;
isPlaying = true;
// 获取视频的宽度和高度
uint width = source.width;
uint height = source.height;
RectTransform parentRect = transform.parent.GetComponent
if (parentRect != null)
{
imageAdaptive.BoundarySize = new Vector2(parentRect.rect.width, parentRect.rect.height);
imageAdaptive.ImageSize = new Vector2(width, height);
}
}
///
private void onVideoEnd(VideoPlayer source)
{
source.Pause();
isPlaying = false;
}
///
public void PlayPauseVideo()
{
if (isPlaying)
{
videoPlayer.Pause();
isPlaying = false;
}
else
{
videoPlayer.Play();
isPlaying = true;
}
}
///
public void setVideoProgress(float progress)
{
if (videoPlayer.canSetTime)
{
videoPlayer.time = videoPlayer.clip.length * progress;
}
}
///
private string formatTime(float time)
{
int minutes = Mathf.FloorToInt(time / 60f);
int seconds = Mathf.FloorToInt(time % 60f);
return string.Format("{0:00}:{1:00}", minutes, seconds);
}
}