303 lines
8.9 KiB
C#
303 lines
8.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Kill.Core;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Video;
|
|
namespace Kill.UI.Components
|
|
{
|
|
public class VideoCtrl : MonoBehaviour
|
|
{
|
|
public bool istest;
|
|
public VideoPlayer videoPlayer;
|
|
|
|
public bool needFullScreen = false;
|
|
bool isFullScreen = false;
|
|
/// <summary>
|
|
/// 0:正常播放 1:全屏播放
|
|
/// </summary>
|
|
public List<RenderTexture> renderTextures;
|
|
public RawImage windowImage;
|
|
public RawImage fullImage;
|
|
public GameObject playButton;
|
|
public GameObject pauseButton;
|
|
public Text totalTimeText;
|
|
public Text nowTime;
|
|
public Slider slider;
|
|
bool isplaying = false;
|
|
bool ispause = true;
|
|
Action playOver;
|
|
string url;
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (isplaying)
|
|
{
|
|
|
|
OnVideoPlaying();
|
|
}
|
|
|
|
}
|
|
public void Init(string url, Action playOver)
|
|
{
|
|
nowTime.text=SecondToHour(0);
|
|
this.playOver = playOver;
|
|
this.url = url;
|
|
SetVideo(url);
|
|
if (playButton != null)
|
|
{
|
|
playButton.SetActive(false);
|
|
}
|
|
if (pauseButton != null)
|
|
{
|
|
pauseButton.SetActive(false);
|
|
}
|
|
ispause = true;
|
|
}
|
|
|
|
public void PlayVideo()
|
|
{
|
|
StartCoroutine(YieldPlay());
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerator YieldPlay()
|
|
{
|
|
yield return new WaitForSeconds(0.1f);
|
|
ispause = false;
|
|
videoPlayer.Play();
|
|
if (playButton != null)
|
|
{
|
|
playButton.SetActive(false);
|
|
}
|
|
if (pauseButton != null)
|
|
{
|
|
pauseButton.SetActive(true);
|
|
}
|
|
isplaying = true;
|
|
}
|
|
public void PauseVideo()
|
|
{
|
|
ispause = true;
|
|
videoPlayer.Pause();
|
|
isplaying = false;
|
|
if (playButton != null)
|
|
{
|
|
playButton.SetActive(true);
|
|
}
|
|
if (pauseButton != null)
|
|
{
|
|
pauseButton.SetActive(false);
|
|
}
|
|
}
|
|
public void RePlay()
|
|
{
|
|
videoPlayer.Stop();
|
|
videoPlayer.frame = 0;
|
|
PlayVideo();
|
|
}
|
|
public void SetVideo(string url)
|
|
{
|
|
videoPlayer.url = url;
|
|
videoPlayer.prepareCompleted += VideoPrepared;
|
|
videoPlayer.Prepare();
|
|
if (playButton != null)
|
|
{
|
|
playButton.SetActive(true);
|
|
}
|
|
}
|
|
public void Stop()
|
|
{
|
|
videoPlayer.Stop();
|
|
videoPlayer.url = "";
|
|
nowTime.text=SecondToHour(0);
|
|
}
|
|
int totalTime = 0;
|
|
public void VideoPrepared(VideoPlayer player)
|
|
{
|
|
if (renderTextures != null && renderTextures.Count > 0)
|
|
{
|
|
foreach (var r in renderTextures)
|
|
Destroy(r);
|
|
renderTextures.Clear();
|
|
}
|
|
renderTextures=new List<RenderTexture>();
|
|
RenderTexture texture = new RenderTexture((int)player.width, (int)player.height, 1);
|
|
renderTextures.Add(texture);
|
|
player.targetTexture = texture;
|
|
windowImage.texture = texture;
|
|
if (needFullScreen)
|
|
{
|
|
var size = fullImage.GetComponentInParent<RectTransform>().rect;
|
|
RenderTexture fullTexture = new RenderTexture((int)size.height, (int)size.width, 1);
|
|
fullImage.GetComponent<RectTransform>().sizeDelta = new Vector2(size.height, size.width);
|
|
renderTextures.Add(fullTexture);
|
|
fullImage.texture = fullTexture;
|
|
}
|
|
videoPlayer.frame = 0;
|
|
if (slider != null)
|
|
{
|
|
totalTime = (int)(player.frameCount / player.frameRate + 0.5f);
|
|
totalTimeText.text = SecondToHour(totalTime);
|
|
}
|
|
|
|
videoPlayer.prepareCompleted -= VideoPrepared;
|
|
|
|
player.loopPointReached += VideoOver;
|
|
if (playButton != null)
|
|
playButton.SetActive(true);
|
|
nowTime.text=SecondToHour(0);
|
|
|
|
}
|
|
public void ShowAnimation(int dir)
|
|
{
|
|
Animation Anim = GetComponent<Animation>();
|
|
if (Anim != null)
|
|
{
|
|
if (dir == -1)
|
|
{
|
|
Anim["视频出现"].time = Anim["视频出现"].clip.length;
|
|
Anim["视频出现"].speed = -1;
|
|
Anim.Play("视频出现");
|
|
}
|
|
else
|
|
{
|
|
Anim["视频出现"].time = 0;
|
|
Anim["视频出现"].speed = 1;
|
|
Anim.Play("视频出现");
|
|
}
|
|
}
|
|
}
|
|
long lastFrame = 0;
|
|
/// <summary>
|
|
/// 播放中事件(给进度条赋值)
|
|
/// </summary>
|
|
|
|
public void OnVideoPlaying()
|
|
{
|
|
if (slider == null)
|
|
return;
|
|
if (Mathf.Abs(videoPlayer.frame - lastFrame) < 1)
|
|
return;
|
|
slider.value = (videoPlayer.frame + 1) / (float)videoPlayer.frameCount;
|
|
lastFrame = videoPlayer.frame;
|
|
nowTime.text = SecondToHour((int)(totalTime * slider.value));
|
|
}
|
|
/// <summary>
|
|
/// 拖动进度条
|
|
/// </summary>
|
|
public void PointDownUp()
|
|
{
|
|
//if (slider.value != lastValue)
|
|
//{
|
|
// videoPlayer.frame = (long)(videoPlayer.frameCount * slider.value);
|
|
//}
|
|
if (slider.value >= 0.98f)
|
|
{
|
|
videoPlayer.Stop();
|
|
VideoOver(videoPlayer);
|
|
return;
|
|
|
|
}
|
|
videoPlayer.frame = (long)(videoPlayer.frameCount * slider.value) - 1;
|
|
nowTime.text = SecondToHour((int)(totalTime * slider.value));
|
|
if (!ispause)
|
|
{
|
|
videoPlayer.Play();
|
|
isplaying = true;
|
|
}
|
|
// TeacherServer.m_instance.SendVideoProcess((long)(videoPlayer.frameCount * slider.value) - 1);
|
|
}
|
|
public void PointDown()
|
|
{
|
|
videoPlayer.Pause();
|
|
isplaying = false;
|
|
}
|
|
public void Draging()
|
|
{
|
|
videoPlayer.frame = (long)(videoPlayer.frameCount * slider.value) - 1;
|
|
nowTime.text = SecondToHour((int)(totalTime * slider.value));
|
|
}
|
|
public void VideoOver(VideoPlayer vp)
|
|
{
|
|
vp.loopPointReached -= VideoOver;
|
|
isplaying = false;
|
|
if (playButton != null)
|
|
{
|
|
playButton.SetActive(true);
|
|
}
|
|
if (pauseButton != null)
|
|
{
|
|
pauseButton.SetActive(false);
|
|
}
|
|
playOver?.Invoke();
|
|
Init(url, playOver);
|
|
}
|
|
|
|
public void SetFullScreen(bool isFullScreen)
|
|
{
|
|
this.isFullScreen = isFullScreen;
|
|
if (isFullScreen)
|
|
{
|
|
#if UNITY_ANDROID
|
|
AndroidStatusBar.statusBarState = AndroidStatusBar.States.Hidden;
|
|
#endif
|
|
videoPlayer.targetTexture = renderTextures[1];
|
|
fullImage.transform.parent.gameObject.SetActive(true);
|
|
UIManager.Instance.AddBackAction(() => SetFullScreen(false));
|
|
if (ispause)
|
|
PlayVideo();
|
|
}
|
|
else
|
|
{
|
|
#if UNITY_ANDROID
|
|
AndroidStatusBar.statusBarState = AndroidStatusBar.States.TranslucentOverContent;
|
|
#endif
|
|
videoPlayer.targetTexture = renderTextures[0];
|
|
fullImage.transform.parent.gameObject.SetActive(false);
|
|
UIManager.Instance.RecoverBackAction();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 秒转换小时
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
/// <returns></returns>
|
|
public string SecondToHour(float time)
|
|
{
|
|
int hour = 0;
|
|
int minute = 0;
|
|
int second = 0;
|
|
second = (int)time;
|
|
|
|
if (second > 60)
|
|
{
|
|
minute = second / 60;
|
|
second = second % 60;
|
|
}
|
|
if (minute > 60)
|
|
{
|
|
hour = minute / 60;
|
|
minute = minute % 60;
|
|
}
|
|
string result = "";
|
|
if (hour > 0)
|
|
{
|
|
result += hour + ":";
|
|
|
|
}
|
|
if (minute < 10)
|
|
result += "0";
|
|
result += minute + ":";
|
|
if (second < 10)
|
|
result += "0";
|
|
result += second.ToString();
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|