using RenderHeads.Media.AVProVideo; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using CG.UTility; using UnityEngine.EventSystems; using ZXK.LouDiXvMuNiu; using System; /******************************************************************************* *Create By CG *Function 视频播放器 *******************************************************************************/ public class VCRCustom : 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 Image _playBigCtrlBtn; [SerializeField] private Button _playVideoCloseBtn; [SerializeField] private GameObject _volume; [SerializeField]//播放暂停小按钮 private Sprite[] _playSmallCtrlImgs; [SerializeField]//播放暂停大按钮 private Sprite[] _playBigCtrlImgs; [SerializeField]//有无声音 private Sprite[] _audioCtrlImgs; private float _setVideoSeekSliderValue; private bool _wasPlayingOnScrub; //private float _setAudioVolumeSliderValue; private GraphicRaycaster _mRaycaster; private EventSystem _mEventSystem; private float _mouseStopTime = 2.0f;//鼠标停留多久隐藏控制按键 private bool _downState = false; private float _curMouseTime = 0.0f; private Vector2 _previousPos = Vector2.zero; private Action _finishHandle; //private string _videoPath = Application.streamingAssetsPath + @"/AVProVideoSamples/BigBuckBunny_720p30.mp4"; void Start() { } public void OnOpenVideoFile(string path) { _audioVolumeSlider.onValueChanged.RemoveAllListeners(); _audioVolumeSlider.onValueChanged.AddListener((vul) => { if (_playingPlayer && _audioVolumeSlider) { GameManager.Instance._CurAppVoice = vul; _playingPlayer.Control.SetVolume(vul); if (GameManager.Instance._CurAppVoice <= 0) { _volume.transform.Find("AudLogo").GetComponent().sprite = _audioCtrlImgs[1]; } else { _volume.transform.Find("AudLogo").GetComponent().sprite = _audioCtrlImgs[0]; } } }); _mRaycaster = GameObject.FindGameObjectWithTag("MainCanvas").GetComponent(); _mEventSystem = GameManager.Instance.transform.GetChild(0).GetComponent(); _playingPlayer.m_VideoPath = path; if (string.IsNullOrEmpty(_playingPlayer.m_VideoPath)) { _playingPlayer.CloseVideo(); } else { _playingPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, _playingPlayer.m_VideoPath, true); _playCtrlBtn.GetComponent().sprite = _playSmallCtrlImgs[0]; } if (_playingPlayer) { _playingPlayer.Events.AddListener(OnVideoEvent); if (_audioVolumeSlider) { if (_playingPlayer.Control != null) { //float volume = _playingPlayer.Control.GetVolume(); //_setAudioVolumeSliderValue = volume; WDebug.Log($"当前音量:{GameManager.Instance._CurAppVoice}"); _audioVolumeSlider.value = GameManager.Instance._CurAppVoice; _playingPlayer.Control.SetVolume(_audioVolumeSlider.value); } } } } public void SetFinishAction(Action finishAnm) { _finishHandle = finishAnm; } 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("mm\\:ss") + "/" + durationSpan.ToString("mm\\:ss");//"hh\\:mm\\:ss" float d = Mathf.Clamp(time / duration, 0.0f, 1.0f); _setVideoSeekSliderValue = d; _videoSeekSlider.value = d; } Vector2 curPos = Input.mousePosition; //监听鼠标是否正在调节声音 GraphicRaycaster(curPos); //监听鼠标是否移动 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: _finishHandle?.Invoke(); break; } WDebug.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 OnPlayPauseButton() { if (_playingPlayer.Control.IsPlaying()) { _playingPlayer.Control.Pause(); _playCtrlBtn.GetComponent().sprite = _playSmallCtrlImgs[1]; _playBigCtrlBtn.GetComponent().sprite = _playBigCtrlImgs[1]; } else { _playingPlayer.Control.Play(); _playCtrlBtn.GetComponent().sprite = _playSmallCtrlImgs[0]; _playBigCtrlBtn.GetComponent().sprite = _playBigCtrlImgs[0]; } } private void CtrlContainerDownUp(bool isDown) { //WDebug.Log($"控制按钮状态;{isDown}"); if (isDown) { if (!_downState) { WDebug.Log("执行下降"); _downState = true; _ctrlContainer.GetComponent().DOFade(0, 0.5f); //_ctrlContainer.DOAnchorPosY(-200.0f, 0.5f); //_playBigCtrlBtn.gameObject.SetActive(false); } } else { if (_downState) { WDebug.Log("执行上升"); _downState = false; _ctrlContainer.GetComponent().DOFade(1, 0.5f); //_ctrlContainer.DOAnchorPosY(-0, 0.5f); //_playBigCtrlBtn.gameObject.SetActive(true); } } } //检测鼠标是否在调节音量图片上面 private void GraphicRaycaster(Vector2 pos) { var mPointerEventData = new PointerEventData(_mEventSystem); mPointerEventData.position = pos; List results = new List(); _mRaycaster.Raycast(mPointerEventData, results); if (results.Count > 2) { for (int i = 0; i < results.Count; i++) { if (results[i].gameObject == _volume.gameObject) { _volume.GetComponent().sizeDelta = new Vector2(30, 180); _audioVolumeSlider.gameObject.SetActive(true); break; } else { _volume.GetComponent().sizeDelta = new Vector2(30, 30); _audioVolumeSlider.gameObject.SetActive(false); } } } } }