You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
9.3 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RenderHeads.Media.AVProVideo;
using UnityEngine.UI;
using LzFramework.UI;
using DG.Tweening;
namespace DisComputer
{
/// <summary>
/// 视频控制器
/// </summary>
public class VideoPlayer : MonoBehaviour
{
public MediaPlayer _mediaPlayer;
public MediaPlayer _mediaPlayerB;
public DisplayUGUI _mediaDisplay;
public RectTransform _bufferedSliderRect;
public Slider _videoSeekSlider;
private float _setVideoSeekSliderValue;
private bool _wasPlayingOnScrub;
public MediaPlayer.FileLocation _location = MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder;
public string _folder = "DisVideo";
public string[] _videoFiles = { ""};
private int _VideoIndex = 0;
private Image _bufferedSliderImage;
public Button _play_btn;
public Button _pause_btn;
public Button _close_btn;
public Button _full_btn;
private MediaPlayer _loadingPlayer;
public MediaPlayer PlayingPlayer
{
get
{
if (LoadingPlayer == _mediaPlayer)
return _mediaPlayerB;
return _mediaPlayer;
}
}
public MediaPlayer LoadingPlayer
{
get
{
return _loadingPlayer;
}
}
private void SwapPlayers()
{
// Pause the previously playing video
PlayingPlayer.Control.Pause();
// Swap the videos
if (LoadingPlayer == _mediaPlayer)
{
_loadingPlayer = _mediaPlayerB;
}
else
{
_loadingPlayer = _mediaPlayer;
}
// Change the displaying video
_mediaDisplay.CurrentMediaPlayer = PlayingPlayer;
}
public void OnOpenVideoFile()
{
LoadingPlayer.m_VideoPath = System.IO.Path.Combine(_folder, _videoFiles[_VideoIndex]);
_VideoIndex = (_VideoIndex + 1) % (_videoFiles.Length);
if (string.IsNullOrEmpty(LoadingPlayer.m_VideoPath))
{
LoadingPlayer.CloseVideo();
_VideoIndex = 0;
}
else
{
LoadingPlayer.OpenVideoFromFile(_location, LoadingPlayer.m_VideoPath);
}
if (_bufferedSliderRect != null)
{
_bufferedSliderImage = _bufferedSliderRect.GetComponent<Image>();
}
}
public void OnPlayButton()
{
if (PlayingPlayer)
{
PlayingPlayer.Control.Play();
}
}
public void OnPauseButton()
{
if (PlayingPlayer)
{
PlayingPlayer.Control.Pause();
}
}
public void OnVideoSeekSlider()
{
if (PlayingPlayer && _videoSeekSlider && _videoSeekSlider.value != _setVideoSeekSliderValue)
{
PlayingPlayer.Control.Seek(_videoSeekSlider.value * PlayingPlayer.Info.GetDurationMs());
}
}
public void OnVideoSliderDown()
{
if (PlayingPlayer)
{
_wasPlayingOnScrub = PlayingPlayer.Control.IsPlaying();
if (_wasPlayingOnScrub)
{
PlayingPlayer.Control.Pause();
}
OnVideoSeekSlider();
}
}
public void OnVideoSliderUp()
{
if (PlayingPlayer && _wasPlayingOnScrub)
{
PlayingPlayer.Control.Play();
_wasPlayingOnScrub = false;
}
}
public void OnRewindButton()
{
if (PlayingPlayer)
{
PlayingPlayer.Control.Rewind();
}
}
private void Awake()
{
_loadingPlayer = _mediaPlayerB;
}
private void OnEnable()
{
if (PlayingPlayer)
{
PlayingPlayer.Events.AddListener(OnVideoEvent);
if (LoadingPlayer)
{
LoadingPlayer.Events.AddListener(OnVideoEvent);
}
OnOpenVideoFile();
}
}
void Start()
{
_full_btn.onClick.AddListener(OnClickFull);
_close_btn.onClick.AddListener(OnClickClose);
_play_btn.onClick.AddListener(OnClickPlay);
_pause_btn.onClick.AddListener(OnClickPause);
}
private void OnDestroy()
{
if (LoadingPlayer)
{
LoadingPlayer.Events.RemoveListener(OnVideoEvent);
}
if (PlayingPlayer)
{
PlayingPlayer.Events.RemoveListener(OnVideoEvent);
}
}
void Update()
{
if (PlayingPlayer && PlayingPlayer.Info != null && PlayingPlayer.Info.GetDurationMs() > 0f)
{
float time = PlayingPlayer.Control.GetCurrentTimeMs();
float duration = PlayingPlayer.Info.GetDurationMs();
float d = Mathf.Clamp(time / duration, 0.0f, 1.0f);
_setVideoSeekSliderValue = d;
_videoSeekSlider.value = d;
if (_bufferedSliderRect != null)
{
if (PlayingPlayer.Control.IsBuffering())
{
float t1 = 0f;
float t2 = PlayingPlayer.Control.GetBufferingProgress();
if (t2 <= 0f)
{
if (PlayingPlayer.Control.GetBufferedTimeRangeCount() > 0)
{
PlayingPlayer.Control.GetBufferedTimeRange(0, ref t1, ref t2);
t1 /= PlayingPlayer.Info.GetDurationMs();
t2 /= PlayingPlayer.Info.GetDurationMs();
}
}
Vector2 anchorMin = Vector2.zero;
Vector2 anchorMax = Vector2.one;
if (_bufferedSliderImage != null &&
_bufferedSliderImage.type == Image.Type.Filled)
{
_bufferedSliderImage.fillAmount = d;
}
else
{
anchorMin[0] = t1;
anchorMax[0] = t2;
}
_bufferedSliderRect.anchorMin = anchorMin;
_bufferedSliderRect.anchorMax = anchorMax;
}
}
}
}
// Callback function to handle events
public void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
{
switch (et)
{
case MediaPlayerEvent.EventType.ReadyToPlay:
break;
case MediaPlayerEvent.EventType.Started:
break;
case MediaPlayerEvent.EventType.FirstFrameReady:
SwapPlayers();
break;
case MediaPlayerEvent.EventType.FinishedPlaying:
break;
}
Debug.Log("Event: " + et.ToString());
}
private void OnClickFull()
{
if (PlayingPlayer)
{
if (PlayingPlayer.Control.IsPlaying())
{
OnPauseButton();
_pause_btn.gameObject.SetActive(true);
_pause_btn.GetComponent<Image>().DOFade(1.0f,0.35f).OnComplete(()=>
{
_pause_btn.GetComponent<Image>().DOFade(.0f, 0.35f).SetDelay(1.0f).OnComplete(()=>
{
_pause_btn.gameObject.SetActive(false);
});
});
}else
{
_play_btn.gameObject.SetActive(true);
_play_btn.GetComponent<Image>().DOFade(1.0f, 0.35f).OnComplete(() =>
{
_play_btn.GetComponent<Image>().DOFade(.0f, 0.35f).SetDelay(1.0f).OnComplete(() =>
{
_play_btn.gameObject.SetActive(false);
});
});
OnPlayButton();
}
}
}
private void OnClickPlay()
{
_play_btn.GetComponent<Image>().DOFade(.0f, 0.0f);
_play_btn.gameObject.SetActive(false);
OnPlayButton();
}
private void OnClickPause()
{
_pause_btn.GetComponent<Image>().DOFade(.0f, 0.0f);
_pause_btn.gameObject.SetActive(false);
OnPauseButton();
}
private void OnClickClose() {
if (PlayingPlayer)
{
PlayingPlayer.CloseVideo();
}
TTUIPage.ClosePage<VideoView>();
}
}
}