using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.Video;
using System;
using System.Net;
using UnityEngine.EventSystems;
using DG.Tweening.Plugins.Options;
namespace QFramework.Example
{
public class UIVideoData : UIPanelData
{
public string url;
public Vector2 offset;
public Vector2 size;
public string finishedEvent;
public string closeEvent;
}
public partial class UIVideo : UIPanel
{
string totalTime;
bool isDragging = false;
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UIVideoData ?? new UIVideoData();
// please add init code here
Progress.onValueChanged.AddListener(OnSliderValueChanged);
Progress.OnBeginDragEvent(OnProgressBeginDrag);
Progress.OnEndDragEvent(OnProgressEndDrag);
player.loopPointReached += VideoPlayer_loopPointReached;
player.prepareCompleted += OnPrepareCompleted;
CloseBtn.onClick.AddListener(() =>
{
if (player.isPlaying)
{
player.Stop();
}
if (string.IsNullOrEmpty(mData.finishedEvent) == false)
{
StringEventSystem.Global.Send(mData.finishedEvent);
}
if (string.IsNullOrEmpty(mData.closeEvent) == false)
{
StringEventSystem.Global.Send(mData.closeEvent);
}
player.targetTexture.Release();
Hide();
});
VideoPlayBtn.onClick.AddListener(() =>
{
player.Pause();
SetImg();
});
PauseBtn.onClick.AddListener(() =>
{
if (player.isPaused)
{
player.Play();
}
else
{
player.Pause();
}
SetImg();
});
}
public void SetImg()
{
if (player.isPaused)
{
PauseImg.gameObject.SetActive(true);
}
else
{
PauseImg.gameObject.SetActive(false);
}
}
private void OnPrepareCompleted(VideoPlayer source)
{
Progress.maxValue = (float)player.length;
totalTime = Utility.FormatTime(player.length);
}
private void OnProgressEndDrag(PointerEventData data)
{
isDragging = false;
player.Play(); // 继续播放视频
}
private void OnProgressBeginDrag(PointerEventData data)
{
isDragging = true;
player.Pause(); // 暂停视频播放
}
///
/// 当进度条的值发生变化时调用
///
/// 进度条的值
private void OnSliderValueChanged(float value)
{
if (isDragging)
{
// 拖动进度条时,设置视频播放时间为进度条的值
player.time = value;
}
}
void Update()
{
if (!isDragging && player.isPlaying)
{
// 更新进度条的值为当前播放时间
Progress.value = (float)player.time;
VideoTimeText.text = $"{Utility.FormatTime(player.time)}/{totalTime}";
}
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UIVideoData ?? new UIVideoData();
if (mData.size != Vector2.zero)
{
VideoContent.rectTransform.sizeDelta = mData.size;
}
VideoContent.transform.localPosition = mData.offset;
player.url = Global.videoPath + mData.url;
player.Play();
SetImg();
}
private void VideoPlayer_loopPointReached(UnityEngine.Video.VideoPlayer source)
{
if (string.IsNullOrEmpty(mData.finishedEvent) == false)
{
StringEventSystem.Global.Send(mData.finishedEvent);
}
player.targetTexture.Release();
}
protected override void OnShow()
{
}
protected override void OnHide()
{
}
protected override void OnClose()
{
}
}
}