1
This commit is contained in:
105
Packages/Core/Interfaces/IPlayer.cs
Normal file
105
Packages/Core/Interfaces/IPlayer.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Timers;
|
||||
using Timer = System.Timers.Timer;
|
||||
namespace BITKit
|
||||
{
|
||||
public enum PlayerState
|
||||
{
|
||||
None,
|
||||
Playing,
|
||||
Paused,
|
||||
}
|
||||
public enum PlayerType
|
||||
{
|
||||
None,
|
||||
Player,
|
||||
Recorder,
|
||||
}
|
||||
public interface IPlayer<TSource>
|
||||
{
|
||||
void Start();
|
||||
void Pause();
|
||||
void Stop();
|
||||
int GetIndex();
|
||||
float GetTime();
|
||||
void SetIndex(int index);
|
||||
void SetTime(float time);
|
||||
PlayerState GetPlayState();
|
||||
}
|
||||
public class Player<T>
|
||||
{
|
||||
public int frameRate = 30;
|
||||
public PlayerState state;
|
||||
protected List<T> list = new();
|
||||
public bool isPlaying;
|
||||
public event Action onStart;
|
||||
public event Action onStop;
|
||||
protected T current;
|
||||
Timer timer = new() { AutoReset = true };
|
||||
public void Start()
|
||||
{
|
||||
if (isPlaying is false)
|
||||
{
|
||||
list ??= new();
|
||||
timer ??= new();
|
||||
isPlaying = true;
|
||||
timer.Interval = TimeSpan.FromSeconds(1f / (float)frameRate).Milliseconds;
|
||||
timer.Elapsed += Update;
|
||||
timer.Start();
|
||||
state = PlayerState.Playing;
|
||||
OnStart();
|
||||
onStart?.Invoke();
|
||||
}
|
||||
}
|
||||
public void Stop()
|
||||
{
|
||||
if (isPlaying)
|
||||
{
|
||||
timer.Stop();
|
||||
timer.Elapsed -= Update;
|
||||
isPlaying = false;
|
||||
state = 0;
|
||||
OnStop();
|
||||
onStop?.Invoke();
|
||||
}
|
||||
}
|
||||
public void Cancel()
|
||||
{
|
||||
timer?.Stop();
|
||||
list?.Clear();
|
||||
onStop?.Invoke();
|
||||
timer = null;
|
||||
isPlaying = false;
|
||||
state = 0;
|
||||
}
|
||||
public void PlayOrPause()
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case PlayerState.None:
|
||||
Start();
|
||||
break;
|
||||
case PlayerState.Playing:
|
||||
state = PlayerState.Paused;
|
||||
break;
|
||||
case PlayerState.Paused:
|
||||
state = PlayerState.Playing;
|
||||
break;
|
||||
}
|
||||
}
|
||||
public void Set(T t)
|
||||
{
|
||||
current = t;
|
||||
}
|
||||
void Update(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
OnUpdate();
|
||||
}
|
||||
protected virtual void OnUpdate() { }
|
||||
protected virtual void OnStart() { }
|
||||
protected virtual void OnStop() { }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user