using RenderHeads.Media.AVProVideo; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using ZXK.ZPS; using DG.Tweening; /******************************************************************************* *Create By CG *Function 视频播放器 *******************************************************************************/ namespace ZXK.UTility { public class VCR : MonoBehaviour { [SerializeField] private MediaPlayer _playingPlayer; [SerializeField] private RectTransform _ctrlContainer; [SerializeField] private Slider _videoSeekSlider; [SerializeField] private Slider _audioVolumeSlider; [SerializeField] private Text _timeText; [SerializeField] private Button _playCtrlBtn; [SerializeField]//播放暂停按钮 private Sprite[] _playCtrlImgs; private float _setVideoSeekSliderValue; private bool _wasPlayingOnScrub; private float _setAudioVolumeSliderValue; private float _mouseStopTime = 2.0f;//鼠标停留多久隐藏控制按键 private bool _downState = false; private float _curMouseTime = 0.0f; private Vector2 _previousPos = Vector2.zero; void Awake() { switch (GameRoot.Instance._CurType) { case EnumCtrl.Type.None: break; case EnumCtrl.Type.WQB: _playingPlayer.m_VideoPath = ConstCtrl.VIDEO_WQB_PATH; break; case EnumCtrl.Type.DHB: _playingPlayer.m_VideoPath = ConstCtrl.VIDEO_DHB_PATH; break; default: break; } } public void OnOpenVideoFile() { if (string.IsNullOrEmpty(_playingPlayer.m_VideoPath)) { _playingPlayer.CloseVideo(); } else { _playingPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, _playingPlayer.m_VideoPath, true); _playCtrlBtn.GetComponent().sprite = _playCtrlImgs[1]; } if (_playingPlayer) { _playingPlayer.Events.AddListener(OnVideoEvent); if (_audioVolumeSlider) { if (_playingPlayer.Control != null) { float volume = _playingPlayer.Control.GetVolume(); _setAudioVolumeSliderValue = volume; Debug.Log($"当前音量:{_setAudioVolumeSliderValue}"); _audioVolumeSlider.value = volume; } } } } void Update() { if (_playingPlayer && _playingPlayer.Info != null && _playingPlayer.Info.GetDurationMs() > 0f) { float time = _playingPlayer.Control.GetCurrentTimeMs(); System.TimeSpan timeSpan = System.TimeSpan.FromMilliseconds(time); float duration = _playingPlayer.Info.GetDurationMs(); System.TimeSpan durationSpan = System.TimeSpan.FromMilliseconds(duration); _timeText.text = timeSpan.ToString("hh\\:mm\\:ss") + "/" + durationSpan.ToString("hh\\:mm\\:ss"); float d = Mathf.Clamp(time / duration, 0.0f, 1.0f); _setVideoSeekSliderValue = d; _videoSeekSlider.value = d; } //监听鼠标是否移动 Vector2 curPos= Input.mousePosition; if (Vector2.Distance(curPos, _previousPos)< 0.5f) { _curMouseTime += Time.deltaTime; } else { _curMouseTime = 0; } if(Input.GetMouseButtonDown(0)|| Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2)) { CtrlContainerDownUp(false); _curMouseTime = 0; } if(_curMouseTime > _mouseStopTime) { CtrlContainerDownUp(true); } else { CtrlContainerDownUp(false); } _previousPos = curPos; } private void OnDestroy() { if (_playingPlayer) { _playingPlayer.Events.RemoveListener(OnVideoEvent); } } 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: break; case MediaPlayerEvent.EventType.FinishedPlaying: break; } Debug.Log("Event: " + et.ToString()); } 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 OnAudioVolumeSlider() { if (_playingPlayer && _audioVolumeSlider && _audioVolumeSlider.value != _setAudioVolumeSliderValue) { _playingPlayer.Control.SetVolume(_audioVolumeSlider.value); } } public void OnPlayPauseButton() { if (_playingPlayer.Control.IsPlaying()) { _playingPlayer.Control.Pause(); _playCtrlBtn.GetComponent().sprite = _playCtrlImgs[0]; } else { _playingPlayer.Control.Play(); _playCtrlBtn.GetComponent().sprite = _playCtrlImgs[1]; } } private void CtrlContainerDownUp(bool isDown) { //Debug.Log($"控制按钮状态;{isDown}"); if (isDown) { if (!_downState) { Debug.Log("执行下降"); _downState = true; _ctrlContainer.DOAnchorPosY(-200.0f, 0.5f); } } else { if (_downState) { Debug.Log("执行上升"); _downState = false; _ctrlContainer.DOAnchorPosY(-0, 0.5f); } } } } }