1
This commit is contained in:
69
Packages/Core/Player/DataPlayer.cs
Normal file
69
Packages/Core/Player/DataPlayer.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user