BITKit/Packages/Core/Player/DataPlayer.cs

70 lines
2.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Timers;
using Timer = System.Timers.Timer;
namespace BITKit
{
public class DataPlayer<T> : Player<T>
{
public int index;
public string timeFormat = "mm:ss:fff";
public event Action<T> output;
public event Action<float> onProgess;
public event Action<string> onSetTotalTime;
public event Action<string> onTime;
float totalTime => (float)list.Count / frameRate;
protected override void OnUpdate()
{
if (list.Count > 0)
{
var progess = (float)index / (float)list.Count;
if (state is PlayerState.Playing)
index = Math.Clamp(++index, 0, list.Count + 32);
if (index > list.Count)
{
if (index - list.Count > frameRate / 2)
Stop();
}
else
{
output?.Invoke(list[index]);
onProgess?.Invoke(progess);
onTime?.Invoke(GetTime(totalTime * progess));
}
}
}
public void Set(List<T> t)
{
Stop();
this.list = new(t);
}
public void SetProgress(float progress)
{
index = (int)(list.Count * progress);
//BIT4Log.Log<DataPlayer<T>>($"设置时间为:{progress}\tIndex:{index}");
}
public void Offset(float time)
{
//32+1/30;
index += (int)(time * frameRate);
}
protected override void OnStart()
{
index = 0;
onSetTotalTime?.Invoke(GetTime(totalTime));
}
protected override void OnStop()
{
base.OnStop();
onTime.Invoke(GetTime(totalTime));
onProgess?.Invoke(1);
}
string GetTime(float sec)
{
return new DateTime().AddSeconds(sec).ToString(timeFormat);
}
}
}