using RenderHeads.Media.AVProVideo;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
public MediaPlayer _MediaPlayer;
public Button PlayBtn, PauseBtn;
public Slider VideoSlider;
bool _isHoveringOverTimeline, _wasPlayingBeforeTimelineDrag;
void Start()
{
CreateTimelineDragEvents();
PlayBtn.onClick.AddListener(Play);
PauseBtn.onClick.AddListener(Pause);
}
#region 视频滑动条
/// <summary>
/// 更新时间滑块位置
/// </summary>
void UpdateTimelineDrag()
{
if (_MediaPlayer.Info != null)
{
TimeRange timelineRange = GetTimelineRange();
if (VideoSlider && !_isHoveringOverTimeline)
{
double t = 0.0;
if (timelineRange.duration > 0.0)
{
t = ((_MediaPlayer.Control.GetCurrentTime() - timelineRange.startTime) / timelineRange.duration);
}
VideoSlider.value = Mathf.Clamp01((float)t);
if (VideoSlider.value == 1)
{
Pause();
PlayBtn.gameObject.SetActive(true);
PauseBtn.gameObject.SetActive(false);
}
}
}
}
void CreateTimelineDragEvents()
{
EventTrigger trigger = VideoSlider.gameObject.GetComponent<EventTrigger>();
if (trigger != null)
{
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerDown;
entry.callback.AddListener((data) => { OnTimeSliderBeginDrag(); });
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;
entry.callback.AddListener((data) => { OnTimeSliderDrag(); });
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerUp;
entry.callback.AddListener((data) => { OnTimeSliderEndDrag(); });
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerEnter;
entry.callback.AddListener((data) => { OnTimelineBeginHover((PointerEventData)data); });
trigger.triggers.Add(entry);
entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerExit;
entry.callback.AddListener((data) => { OnTimelineEndHover((PointerEventData)data); });
trigger.triggers.Add(entry);
}
}
private void OnTimeSliderBeginDrag()
{
if (_MediaPlayer && _MediaPlayer.Control != null)
{
_wasPlayingBeforeTimelineDrag = _MediaPlayer.Control.IsPlaying();
if (_wasPlayingBeforeTimelineDrag)
{
_MediaPlayer.Pause();
}
OnTimeSliderDrag();
}
}
private void OnTimeSliderDrag()
{
if (_MediaPlayer && _MediaPlayer.Control != null)
{
TimeRange timelineRange = GetTimelineRange();
double time = timelineRange.startTime + (VideoSlider.value * timelineRange.duration);
_MediaPlayer.Control.Seek(time);
_isHoveringOverTimeline = true;
}
}
private void OnTimeSliderEndDrag()
{
if (_MediaPlayer && _MediaPlayer.Control != null)
{
if (_wasPlayingBeforeTimelineDrag)
{
_MediaPlayer.Play();
_wasPlayingBeforeTimelineDrag = false;
}
}
}
private TimeRange GetTimelineRange()
{
if (_MediaPlayer.Info != null)
{
return Helper.GetTimelineRange(_MediaPlayer.Info.GetDuration(), _MediaPlayer.Control.GetSeekableTimes());
}
return new TimeRange();
}
private void OnTimelineBeginHover(PointerEventData eventData)
{
if (eventData.pointerCurrentRaycast.gameObject != null)
{
_isHoveringOverTimeline = true;
}
}
private void OnTimelineEndHover(PointerEventData eventData)
{
_isHoveringOverTimeline = false;
}
private void Play()
{
if (VideoSlider.value == 1)
{
_MediaPlayer.Rewind(true);
}
_MediaPlayer.Play();
}
private void Pause()
{
_MediaPlayer.AutoStart = false;
_MediaPlayer.Pause();
}
#endregion
void Update()
{
UpdateTimelineDrag();
}
}
AVPro插件视频滑动条
最新推荐文章于 2024-08-21 23:25:07 发布