1
This commit is contained in:
21
Unity/Scripts/MediaPlayer/BITKit.MediaPlayer.asmdef
Normal file
21
Unity/Scripts/MediaPlayer/BITKit.MediaPlayer.asmdef
Normal 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
|
||||
}
|
69
Unity/Scripts/MediaPlayer/MediaPlayer.cs
Normal file
69
Unity/Scripts/MediaPlayer/MediaPlayer.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
140
Unity/Scripts/MediaPlayer/VideoPlayer.cs
Normal file
140
Unity/Scripts/MediaPlayer/VideoPlayer.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user