using System; using System.Collections; using System.Collections.Generic; using System.Timers; using Timer = System.Timers.Timer; namespace BITKit { public class DataPlayer : Player { public int index; public string timeFormat = "mm:ss:fff"; public event Action output; public event Action onProgess; public event Action onSetTotalTime; public event Action 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) { Stop(); this.list = new(t); } public void SetProgress(float progress) { index = (int)(list.Count * progress); //BIT4Log.Log>($"设置时间为:{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); } } }