This commit is contained in:
CortexCore
2023-06-05 16:25:06 +08:00
parent 9027120bb8
commit 4565ff2e35
2947 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
{
"name": "BITKit.MediaPlayer",
"rootNamespace": "",
"references": [
"GUID:a209c53514018594f9f482516f2a6781",
"GUID:f51ebe6a0ceec4240a699833d6309b23",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:be17a8778dbfe454890ed8279279e153",
"GUID:14fe60d984bf9f84eac55c6ea033a8f4"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,69 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using BITKit;
using UnityEngine.Video;
namespace BITKit.Media
{
public abstract class MediaPlayer<TPlayer> : Provider
{
[Header(Constant.Header.Settings)]
public bool playOnStart;
[Header(Constant.Header.State)]
public bool isPlaying;
public float currentTime;
[Header(Constant.Header.Components)]
public TPlayer player;
[Header(Constant.Header.Events)]
public UnityEvent onStartPlay = new();
public UnityEvent onStopPlay = new();
public UnityEvent onPlay = new();
public UnityEvent onPause = new();
public UnityEvent<float> onSetTotalTime;
public UnityEvent<string> onSetTotalTimeAsString;
public UnityEvent<float> onTime;
public UnityEvent<string> onTimeAsString;
protected virtual void OnStart()
{
}
public virtual void Play()
{
}
public virtual void Stop()
{
}
public virtual void PlayOrPause()
{
}
public virtual void UpdateProgess()
{
}
public virtual void SetNormalizeTime(float time)
{
}
void Start()
{
OnStart();
if (playOnStart)
{
Play();
}
}
void Update()
{
if (isPlaying)
{
UpdateProgess();
}
}
}
}

View File

@@ -0,0 +1,140 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
using UnityEngine.InputSystem;
using Cysharp.Threading.Tasks;
using System.Threading.Tasks;
namespace BITKit.Media
{
public class VideoPlayer : MediaPlayer<UnityEngine.Video.VideoPlayer>
{
const string timeFormat = "HH:mm:ss";
[Header(Constant.Header.Settings)]
public InputAction playOrPauseAction;
public bool log;
[Header(Constant.Header.Debug)]
public string currentTimeAsString;
void Enable()
{
playOrPauseAction.Enable();
}
void Disable()
{
playOrPauseAction.Disable();
}
protected override void OnStart()
{
playOrPauseAction.performed += OnPlayOrPause;
Prepare();
}
public override void Play()
{
if (isPlaying is false)
{
if (log)
{
BIT4Log.Log<VideoPlayer>($"开始播放视频:{player.clip.name}");
}
DateTime totalDateTime = new();
player.Play();
isPlaying = true;
onStartPlay.Invoke();
totalDateTime = totalDateTime.AddSeconds(player.length);
onSetTotalTime.Invoke((float)player.length);
onSetTotalTimeAsString.Invoke(totalDateTime.ToString(timeFormat));
}
else
{
PlayOrPause();
}
}
public override void Stop()
{
if (isPlaying)
{
player.Stop();
isPlaying = false;
onStopPlay.Invoke();
}
}
public void Pause()
{
if (isPlaying)
{
if (player.isPaused is false)
{
onPause.Invoke();
player.Pause();
}
}
}
public override void PlayOrPause()
{
if (isPlaying)
{
if (player.isPaused)
{
onPlay.Invoke();
player.Play();
}
else
{
Pause();
}
}
else
{
Play();
}
}
public override void UpdateProgess()
{
if (player.isPlaying && player.isPaused is false)
{
DateTime playDateTime = new();
var doubleTime = player.time / player.length;
currentTime = (float)doubleTime;
playDateTime = playDateTime.AddSeconds(player.time);
onTime.Invoke(currentTime);
currentTimeAsString = playDateTime.ToString(timeFormat);
onTimeAsString.Invoke(currentTimeAsString);
}
}
public override void SetNormalizeTime(float time)
{
player.time = Mathf.Lerp(0, (float)player.length, time);
}
public override void Set<T>(T obj)
{
switch (obj)
{
case float time:
SetNormalizeTime(time);
break;
}
}
public async void Prepare()
{
player.Prepare();
Stop();
SetNormalizeTime(0);
Play();
await Task.Delay(128, gameObject.GetCancellationTokenOnDestroy());
Stop();
SetNormalizeTime(0);
}
void OnPlayOrPause(InputAction.CallbackContext context)
{
Debug.Log(context);
PlayOrPause();
}
}
}