using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RenderHeads.Media.AVProVideo;
using UnityEngine.UI;
public class VideoManager : MonoBehaviour
{
public MediaPlayer videoPlayer;
public Slider slider;
[SerializeField]
float totalDuration; // 视频总时长,单位为秒
public Text durationText; // 显示视频时长的UI Text组件
public Text nowText; // 显示视频时长的UI Text组件
double time = 0;
private void Start()
{
videoPlayer.Events.AddListener(OnMediaPlayerEvent);
}
private void OnMediaPlayerEvent(MediaPlayer player, MediaPlayerEvent.EventType eventType, ErrorCode errorCode)
{
if (eventType == MediaPlayerEvent.EventType.FirstFrameReady)
{
InvokeRepeating("UpdateProgressBar", 0f, 0.1f); // 每0.1秒更新一次进度条
}
}
public void OnVideoSeekSlider1()
{
videoPlayer.Control.Seek(slider.value * videoPlayer.Info.GetDurationMs());
}
void UpdateProgressBar()
{
totalDuration = (float)videoPlayer.Info.GetDurationMs()/1000;
int hours = (int)(totalDuration / 3600);
int minutes = (int)((totalDuration % 3600) / 60);
int seconds = (int)(totalDuration % 60);
durationText.text = string.Format("{1:D1}:{2:D2}", hours, minutes, seconds);
int cut = (int)(videoPlayer.Control.GetCurrentTimeMs()/1000);
int minutes2 = ((cut % 3600) / 60);
int seconds2 = (cut % 60);
nowText.text = string.Format("{1:D1}:{2:D2}", hours, minutes2, seconds2);
if (videoPlayer && videoPlayer.Info != null)
{
time = videoPlayer.Control.GetCurrentTimeMs() ;
if (videoPlayer.Info.GetDurationMs() <= 0)
{
return;
}
double vol = (time / videoPlayer.Info.GetDurationMs());
slider.SetValueWithoutNotify((float)vol);
}
}
}