MediaPlayerUI.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. // UnityEngine.UI was moved to a package in 2019.2.0
  2. // Unfortunately no way to test for this across all Unity versions yet
  3. // You can set up the asmdef to reference the new package, but the package doesn't
  4. // existing in Unity 2017 etc, and it throws an error due to missing reference
  5. #define AVPRO_PACKAGE_UNITYUI
  6. #if (UNITY_2019_2_OR_NEWER && AVPRO_PACKAGE_UNITYUI) || (!UNITY_2019_2_OR_NEWER)
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using UnityEngine.EventSystems;
  12. using RenderHeads.Media.AVProVideo;
  13. using RenderHeads.Media.AVProVideo.Demos.UI;
  14. //-----------------------------------------------------------------------------
  15. // Copyright 2018-2021 RenderHeads Ltd. All rights reserved.
  16. //-----------------------------------------------------------------------------
  17. namespace RenderHeads.Media.AVProVideo.Demos
  18. {
  19. public class MediaPlayerUI : MonoBehaviour
  20. {
  21. [SerializeField] MediaPlayer _mediaPlayer = null;
  22. [Header("Options")]
  23. [SerializeField] float _keyVolumeDelta = 0.05f;
  24. [SerializeField] float _jumpDeltaTime = 5f;
  25. [SerializeField] bool _showOptions = true;
  26. [SerializeField] bool _autoHide = true;
  27. [SerializeField] float _userInactiveDuration = 1.5f;
  28. [SerializeField] bool _useAudioFading = true;
  29. [Header("Keyboard Controls")]
  30. [SerializeField] bool _enableKeyboardControls = true;
  31. [SerializeField] KeyCode KeyVolumeUp = KeyCode.UpArrow;
  32. [SerializeField] KeyCode KeyVolumeDown = KeyCode.DownArrow;
  33. [SerializeField] KeyCode KeyTogglePlayPause = KeyCode.Space;
  34. [SerializeField] KeyCode KeyToggleMute = KeyCode.M;
  35. [SerializeField] KeyCode KeyJumpForward = KeyCode.RightArrow;
  36. [SerializeField] KeyCode KeyJumpBack = KeyCode.LeftArrow;
  37. [Header("Optional Components")]
  38. [SerializeField] OverlayManager _overlayManager = null;
  39. [SerializeField] MediaPlayer _thumbnailMediaPlayer = null;
  40. [SerializeField] RectTransform _timelineTip = null;
  41. [Header("UI Components")]
  42. [SerializeField] RectTransform _canvasTransform = null;
  43. //[SerializeField] Image image = null;
  44. [SerializeField] Slider _sliderTime = null;
  45. [SerializeField] EventTrigger _videoTouch = null;
  46. [SerializeField] CanvasGroup _controlsGroup = null;
  47. [Header("UI Components (Optional)")]
  48. [SerializeField] GameObject _liveItem = null;
  49. [SerializeField] Text _textMediaName = null;
  50. [SerializeField] Text _textTimeDuration = null;
  51. [SerializeField] Slider _sliderVolume = null;
  52. [SerializeField] Button _buttonPlayPause = null;
  53. [SerializeField] Button _buttonVolume = null;
  54. [SerializeField] Button _buttonSubtitles = null;
  55. [SerializeField] Button _buttonOptions = null;
  56. [SerializeField] Button _buttonTimeBack = null;
  57. [SerializeField] Button _buttonTimeForward = null;
  58. [SerializeField] RawImage _imageAudioSpectrum = null;
  59. [SerializeField] GameObject _optionsMenuRoot = null;
  60. [SerializeField] HorizontalSegmentsPrimitive _segmentsSeek = null;
  61. [SerializeField] HorizontalSegmentsPrimitive _segmentsBuffered = null;
  62. [SerializeField] HorizontalSegmentsPrimitive _segmentsProgress = null;
  63. private bool _wasPlayingBeforeTimelineDrag;
  64. private float _controlsFade = 1f;
  65. private Material _playPauseMaterial;
  66. private Material _volumeMaterial;
  67. private Material _subtitlesMaterial;
  68. private Material _optionsMaterial;
  69. private Material _audioSpectrumMaterial;
  70. private float[] _spectrumSamples = new float[128];
  71. private float[] _spectrumSamplesSmooth = new float[128];
  72. private float _maxValue = 1f;
  73. private float _audioVolume = 1f;
  74. private float _audioFade = 0f;
  75. private bool _isAudioFadingUpToPlay = true;
  76. private const float AudioFadeDuration = 0.25f;
  77. private float _audioFadeTime = 0f;
  78. private readonly LazyShaderProperty _propMorph = new LazyShaderProperty("_Morph");
  79. private readonly LazyShaderProperty _propMute = new LazyShaderProperty("_Mute");
  80. private readonly LazyShaderProperty _propVolume = new LazyShaderProperty("_Volume");
  81. private readonly LazyShaderProperty _propSpectrum = new LazyShaderProperty("_Spectrum");
  82. private readonly LazyShaderProperty _propSpectrumRange = new LazyShaderProperty("_SpectrumRange");
  83. void Awake()
  84. {
  85. #if UNITY_IOS
  86. Application.targetFrameRate = 60;
  87. #endif
  88. }
  89. void Start()
  90. {
  91. if (_mediaPlayer)
  92. {
  93. _audioVolume = _mediaPlayer.AudioVolume;
  94. }
  95. SetupPlayPauseButton();
  96. SetupTimeBackForwardButtons();
  97. SetupVolumeButton();
  98. SetupSubtitlesButton();
  99. SetupOptionsButton();
  100. SetupAudioSpectrum();
  101. CreateTimelineDragEvents();
  102. CreateVideoTouchEvents();
  103. CreateVolumeSliderEvents();
  104. UpdateVolumeSlider();
  105. BuildOptionsMenu();
  106. }
  107. private struct UserInteraction
  108. {
  109. public static float InactiveTime;
  110. private static Vector3 _previousMousePos;
  111. private static int _lastInputFrame;
  112. public static bool IsUserInputThisFrame()
  113. {
  114. if (Time.frameCount == _lastInputFrame)
  115. {
  116. return true;
  117. }
  118. #if (!ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER)
  119. bool touchInput = (Input.touchSupported && Input.touchCount > 0);
  120. bool mouseInput = (Input.mousePresent && (Input.mousePosition != _previousMousePos || Input.mouseScrollDelta != Vector2.zero || Input.GetMouseButton(0)));
  121. if (touchInput || mouseInput)
  122. {
  123. _previousMousePos = Input.mousePosition;
  124. _lastInputFrame = Time.frameCount;
  125. return true;
  126. }
  127. return false;
  128. #else
  129. return true;
  130. #endif
  131. }
  132. }
  133. private Material DuplicateMaterialOnImage(Graphic image)
  134. {
  135. // Assign a copy of the material so we aren't modifying the material asset file
  136. image.material = new Material(image.material);
  137. return image.material;
  138. }
  139. private void SetupPlayPauseButton()
  140. {
  141. if (_buttonPlayPause)
  142. {
  143. _buttonPlayPause.onClick.AddListener(OnPlayPauseButtonPressed);
  144. _playPauseMaterial = DuplicateMaterialOnImage(_buttonPlayPause.GetComponent<Image>());
  145. }
  146. }
  147. private void SetupTimeBackForwardButtons()
  148. {
  149. if (_buttonTimeBack)
  150. {
  151. _buttonTimeBack.onClick.AddListener(OnPlayTimeBackButtonPressed);
  152. }
  153. if (_buttonTimeForward)
  154. {
  155. _buttonTimeForward.onClick.AddListener(OnPlayTimeForwardButtonPressed);
  156. }
  157. }
  158. private void SetupVolumeButton()
  159. {
  160. if (_buttonVolume)
  161. {
  162. _buttonVolume.onClick.AddListener(OnVolumeButtonPressed);
  163. _volumeMaterial = DuplicateMaterialOnImage(_buttonVolume.GetComponent<Image>());
  164. }
  165. }
  166. private void SetupSubtitlesButton()
  167. {
  168. if (_buttonSubtitles)
  169. {
  170. _buttonSubtitles.onClick.AddListener(OnSubtitlesButtonPressed);
  171. _subtitlesMaterial = DuplicateMaterialOnImage(_buttonSubtitles.GetComponent<Image>());
  172. }
  173. }
  174. private void SetupOptionsButton()
  175. {
  176. if (_buttonOptions)
  177. {
  178. _buttonOptions.onClick.AddListener(OnOptionsButtonPressed);
  179. _optionsMaterial = DuplicateMaterialOnImage(_buttonOptions.GetComponent<Image>());
  180. }
  181. }
  182. private void SetupAudioSpectrum()
  183. {
  184. if (_imageAudioSpectrum)
  185. {
  186. _audioSpectrumMaterial = DuplicateMaterialOnImage(_imageAudioSpectrum);
  187. }
  188. }
  189. private void OnPlayPauseButtonPressed()
  190. {
  191. TogglePlayPause();
  192. }
  193. private void OnPlayTimeBackButtonPressed()
  194. {
  195. SeekRelative(-_jumpDeltaTime);
  196. }
  197. private void OnPlayTimeForwardButtonPressed()
  198. {
  199. SeekRelative(_jumpDeltaTime);
  200. }
  201. private void OnVolumeButtonPressed()
  202. {
  203. ToggleMute();
  204. }
  205. private void OnSubtitlesButtonPressed()
  206. {
  207. ToggleSubtitles();
  208. }
  209. private void OnOptionsButtonPressed()
  210. {
  211. ToggleOptionsMenu();
  212. }
  213. private bool _isHoveringOverTimeline;
  214. private void OnTimelineBeginHover(PointerEventData eventData)
  215. {
  216. if (eventData.pointerCurrentRaycast.gameObject != null)
  217. {
  218. _isHoveringOverTimeline = true;
  219. _sliderTime.transform.localScale = new Vector3(1f, 2.5f, 1f);
  220. }
  221. }
  222. private void OnTimelineEndHover(PointerEventData eventData)
  223. {
  224. _isHoveringOverTimeline = false;
  225. _sliderTime.transform.localScale = new Vector3(1f, 1f, 1f);
  226. }
  227. private void CreateVideoTouchEvents()
  228. {
  229. EventTrigger.Entry entry = new EventTrigger.Entry();
  230. entry.eventID = EventTriggerType.PointerUp;
  231. entry.callback.AddListener((data) => { OnVideoPointerUp(); });
  232. _videoTouch.triggers.Add(entry);
  233. }
  234. private void OnVideoPointerUp()
  235. {
  236. bool controlsMostlyVisible = (_controlsGroup.alpha >= 0.5f && _controlsGroup.gameObject.activeSelf);
  237. if (controlsMostlyVisible)
  238. {
  239. TogglePlayPause();
  240. }
  241. }
  242. void UpdateAudioFading()
  243. {
  244. // Increment fade timer
  245. if (_audioFadeTime < AudioFadeDuration)
  246. {
  247. _audioFadeTime = Mathf.Clamp(_audioFadeTime + Time.deltaTime, 0f, AudioFadeDuration);
  248. }
  249. // Trigger pause when audio faded down
  250. if (_audioFadeTime >= AudioFadeDuration)
  251. {
  252. if (!_isAudioFadingUpToPlay)
  253. {
  254. Pause(skipFeedback:true);
  255. }
  256. }
  257. // Apply audio fade value
  258. if (_mediaPlayer.Control != null && _mediaPlayer.Control.IsPlaying())
  259. {
  260. _audioFade = Mathf.Clamp01(_audioFadeTime / AudioFadeDuration);
  261. if (!_isAudioFadingUpToPlay)
  262. {
  263. _audioFade = (1f - _audioFade);
  264. }
  265. ApplyAudioVolume();
  266. }
  267. }
  268. public void TogglePlayPause()
  269. {
  270. if (_mediaPlayer && _mediaPlayer.Control != null)
  271. {
  272. if (_useAudioFading && _mediaPlayer.Info.HasAudio())
  273. {
  274. if (_mediaPlayer.Control.IsPlaying())
  275. {
  276. if (_overlayManager)
  277. {
  278. _overlayManager.TriggerFeedback(OverlayManager.Feedback.Pause);
  279. }
  280. _isAudioFadingUpToPlay = false;
  281. }
  282. else
  283. {
  284. _isAudioFadingUpToPlay = true;
  285. Play();
  286. }
  287. _audioFadeTime = 0f;
  288. }
  289. else
  290. {
  291. if (_mediaPlayer.Control.IsPlaying())
  292. {
  293. Pause();
  294. }
  295. else
  296. {
  297. Play();
  298. }
  299. }
  300. }
  301. }
  302. private void Play()
  303. {
  304. if (_mediaPlayer && _mediaPlayer.Control != null)
  305. {
  306. if (_overlayManager)
  307. {
  308. _overlayManager.TriggerFeedback(OverlayManager.Feedback.Play);
  309. }
  310. _mediaPlayer.Play();
  311. }
  312. }
  313. private void Pause(bool skipFeedback = false)
  314. {
  315. if (_mediaPlayer && _mediaPlayer.Control != null)
  316. {
  317. if (!skipFeedback)
  318. {
  319. if (_overlayManager)
  320. {
  321. _overlayManager.TriggerFeedback(OverlayManager.Feedback.Pause);
  322. }
  323. }
  324. _mediaPlayer.Pause();
  325. }
  326. }
  327. public void SeekRelative(float deltaTime)
  328. {
  329. if (_mediaPlayer && _mediaPlayer.Control != null)
  330. {
  331. TimeRange timelineRange = GetTimelineRange();
  332. double time = _mediaPlayer.Control.GetCurrentTime() + deltaTime;
  333. time = System.Math.Max(time, timelineRange.startTime);
  334. time = System.Math.Min(time, timelineRange.startTime + timelineRange.duration);
  335. _mediaPlayer.Control.Seek(time);
  336. if (_overlayManager)
  337. {
  338. _overlayManager.TriggerFeedback(deltaTime > 0f ? OverlayManager.Feedback.SeekForward : OverlayManager.Feedback.SeekBack);
  339. }
  340. }
  341. }
  342. public void ChangeAudioVolume(float delta)
  343. {
  344. if (_mediaPlayer && _mediaPlayer.Control != null)
  345. {
  346. // Change volume
  347. _audioVolume = Mathf.Clamp01(_audioVolume + delta);
  348. // Update the UI
  349. UpdateVolumeSlider();
  350. // Trigger the overlays
  351. if (_overlayManager)
  352. {
  353. _overlayManager.TriggerFeedback(delta > 0f ? OverlayManager.Feedback.VolumeUp : OverlayManager.Feedback.VolumeDown);
  354. }
  355. }
  356. }
  357. public void ToggleMute()
  358. {
  359. if (_mediaPlayer && _mediaPlayer.Control != null)
  360. {
  361. if (_mediaPlayer.AudioMuted)
  362. {
  363. MuteAudio(false);
  364. }
  365. else
  366. {
  367. MuteAudio(true);
  368. }
  369. }
  370. }
  371. private void MuteAudio(bool mute)
  372. {
  373. if (_mediaPlayer && _mediaPlayer.Control != null)
  374. {
  375. // Change mute
  376. _mediaPlayer.AudioMuted = mute;
  377. // Update the UI
  378. // The UI element is constantly updated by the Update() method
  379. // Trigger the overlays
  380. if (_overlayManager)
  381. {
  382. _overlayManager.TriggerFeedback(mute ? OverlayManager.Feedback.VolumeMute : OverlayManager.Feedback.VolumeUp);
  383. }
  384. }
  385. }
  386. public void ToggleSubtitles()
  387. {
  388. if (_mediaPlayer && _mediaPlayer.TextTracks != null)
  389. {
  390. if (_mediaPlayer.TextTracks.GetTextTracks().Count > 0)
  391. {
  392. if (_mediaPlayer.TextTracks.GetActiveTextTrack() != null)
  393. {
  394. _mediaPlayer.TextTracks.SetActiveTextTrack(null);
  395. }
  396. else
  397. {
  398. // TODO: instead of activating the first one, base it on the language/track
  399. // selection stored in the MediaPlayerUI
  400. _mediaPlayer.TextTracks.SetActiveTextTrack(_mediaPlayer.TextTracks.GetTextTracks()[0]);
  401. }
  402. }
  403. }
  404. }
  405. private void ToggleOptionsMenu()
  406. {
  407. _showOptions = !_showOptions;
  408. BuildOptionsMenu();
  409. }
  410. private void BuildOptionsMenu()
  411. {
  412. if (_optionsMenuRoot)
  413. {
  414. _optionsMenuRoot.SetActive(_showOptions);
  415. }
  416. // Temporary code for now disables to touch controls while the debug menu
  417. // is shown, to stop it consuming mouse input for IMGUI
  418. _videoTouch.enabled = !_showOptions;
  419. }
  420. private void CreateTimelineDragEvents()
  421. {
  422. EventTrigger trigger = _sliderTime.gameObject.GetComponent<EventTrigger>();
  423. if (trigger != null)
  424. {
  425. EventTrigger.Entry entry = new EventTrigger.Entry();
  426. entry.eventID = EventTriggerType.PointerDown;
  427. entry.callback.AddListener((data) => { OnTimeSliderBeginDrag(); });
  428. trigger.triggers.Add(entry);
  429. entry = new EventTrigger.Entry();
  430. entry.eventID = EventTriggerType.Drag;
  431. entry.callback.AddListener((data) => { OnTimeSliderDrag(); });
  432. trigger.triggers.Add(entry);
  433. entry = new EventTrigger.Entry();
  434. entry.eventID = EventTriggerType.PointerUp;
  435. entry.callback.AddListener((data) => { OnTimeSliderEndDrag(); });
  436. trigger.triggers.Add(entry);
  437. entry = new EventTrigger.Entry();
  438. entry.eventID = EventTriggerType.PointerEnter;
  439. entry.callback.AddListener((data) => { OnTimelineBeginHover((PointerEventData)data); });
  440. trigger.triggers.Add(entry);
  441. entry = new EventTrigger.Entry();
  442. entry.eventID = EventTriggerType.PointerExit;
  443. entry.callback.AddListener((data) => { OnTimelineEndHover((PointerEventData)data); });
  444. trigger.triggers.Add(entry);
  445. }
  446. }
  447. private void CreateVolumeSliderEvents()
  448. {
  449. if (_sliderVolume != null)
  450. {
  451. EventTrigger trigger = _sliderVolume.gameObject.GetComponent<EventTrigger>();
  452. if (trigger != null)
  453. {
  454. EventTrigger.Entry entry = new EventTrigger.Entry();
  455. entry.eventID = EventTriggerType.PointerDown;
  456. entry.callback.AddListener((data) => { OnVolumeSliderDrag(); });
  457. trigger.triggers.Add(entry);
  458. entry = new EventTrigger.Entry();
  459. entry.eventID = EventTriggerType.Drag;
  460. entry.callback.AddListener((data) => { OnVolumeSliderDrag(); });
  461. trigger.triggers.Add(entry);
  462. }
  463. }
  464. }
  465. private void OnVolumeSliderDrag()
  466. {
  467. if (_mediaPlayer && _mediaPlayer.Control != null)
  468. {
  469. _audioVolume = _sliderVolume.value;
  470. ApplyAudioVolume();
  471. }
  472. }
  473. private void ApplyAudioVolume()
  474. {
  475. if (_mediaPlayer)
  476. {
  477. _mediaPlayer.AudioVolume = (_audioVolume * _audioFade);
  478. }
  479. }
  480. private void UpdateVolumeSlider()
  481. {
  482. if (_sliderVolume)
  483. {
  484. if (_mediaPlayer)
  485. {
  486. // TODO: remove this
  487. /*if (mp.Control != null)
  488. {
  489. _sliderVolume.value = mp.Control.GetVolume();
  490. }
  491. else*/
  492. {
  493. _sliderVolume.value = _audioVolume;
  494. }
  495. }
  496. }
  497. }
  498. private void UpdateAudioSpectrum()
  499. {
  500. bool showAudioSpectrum = false;
  501. if (_mediaPlayer && _mediaPlayer.Control != null)
  502. {
  503. AudioSource audioSource = _mediaPlayer.AudioSource;
  504. if (audioSource && _audioSpectrumMaterial)
  505. {
  506. showAudioSpectrum = true;
  507. float maxFreq = (Helper.GetUnityAudioSampleRate() / 2);
  508. // Frequencies over 18Khz generally aren't very interesting to visualise, so clamp the range
  509. const float clampFreq = 18000f;
  510. int sampleRange = Mathf.FloorToInt(Mathf.Clamp01(clampFreq / maxFreq) * _spectrumSamples.Length);
  511. // Add new samples and smooth the samples over time
  512. audioSource.GetSpectrumData(_spectrumSamples, 0, FFTWindow.BlackmanHarris);
  513. // Find the maxValue sample for normalising with
  514. float maxValue = -1.0f;
  515. for (int i = 0; i < sampleRange; i++)
  516. {
  517. if (_spectrumSamples[i] > maxValue)
  518. {
  519. maxValue = _spectrumSamples[i];
  520. }
  521. }
  522. // Chase maxValue to zero
  523. _maxValue = Mathf.Lerp(_maxValue, 0.0f, Mathf.Clamp01(2.0f * Time.deltaTime));
  524. // Update maxValue
  525. _maxValue = Mathf.Max(_maxValue, maxValue);
  526. if (_maxValue <= 0.01f)
  527. {
  528. _maxValue = 1f;
  529. }
  530. // Copy and smooth the spectrum values
  531. for (int i = 0; i < sampleRange; i++)
  532. {
  533. float newSample = _spectrumSamples[i] / _maxValue;
  534. _spectrumSamplesSmooth[i] = Mathf.Lerp(_spectrumSamplesSmooth[i], newSample, Mathf.Clamp01(15.0f * Time.deltaTime));
  535. }
  536. // Update shader
  537. _audioSpectrumMaterial.SetFloatArray(_propSpectrum.Id, _spectrumSamplesSmooth);
  538. _audioSpectrumMaterial.SetFloat(_propSpectrumRange.Id, (float)sampleRange);
  539. }
  540. }
  541. if (_imageAudioSpectrum)
  542. {
  543. _imageAudioSpectrum.gameObject.SetActive(showAudioSpectrum);
  544. }
  545. }
  546. private void OnTimeSliderBeginDrag()
  547. {
  548. if (_mediaPlayer && _mediaPlayer.Control != null)
  549. {
  550. _wasPlayingBeforeTimelineDrag = _mediaPlayer.Control.IsPlaying();
  551. if (_wasPlayingBeforeTimelineDrag)
  552. {
  553. _mediaPlayer.Pause();
  554. }
  555. OnTimeSliderDrag();
  556. }
  557. }
  558. private void OnTimeSliderDrag()
  559. {
  560. if (_mediaPlayer && _mediaPlayer.Control != null)
  561. {
  562. TimeRange timelineRange = GetTimelineRange();
  563. double time = timelineRange.startTime + (_sliderTime.value * timelineRange.duration);
  564. _mediaPlayer.Control.Seek(time);
  565. _isHoveringOverTimeline = true;
  566. }
  567. }
  568. private void OnTimeSliderEndDrag()
  569. {
  570. if (_mediaPlayer && _mediaPlayer.Control != null)
  571. {
  572. if (_wasPlayingBeforeTimelineDrag)
  573. {
  574. _mediaPlayer.Play();
  575. _wasPlayingBeforeTimelineDrag = false;
  576. }
  577. }
  578. }
  579. private TimeRange GetTimelineRange()
  580. {
  581. if (_mediaPlayer.Info != null)
  582. {
  583. return Helper.GetTimelineRange(_mediaPlayer.Info.GetDuration(), _mediaPlayer.Control.GetSeekableTimes());
  584. }
  585. return new TimeRange();
  586. }
  587. private bool CanHideControls()
  588. {
  589. bool result = true;
  590. if (!_autoHide)
  591. {
  592. result = false;
  593. }
  594. #if (!ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER)
  595. else if (Input.mousePresent)
  596. {
  597. // Check whether the mouse cursor is over the controls, in which case we can't hide the UI
  598. RectTransform rect = _controlsGroup.GetComponent<RectTransform>();
  599. Vector2 canvasPos;
  600. RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, Input.mousePosition, null, out canvasPos);
  601. Rect rr = RectTransformUtility.PixelAdjustRect(rect, null);
  602. result = !rr.Contains(canvasPos);
  603. }
  604. #endif
  605. return result;
  606. }
  607. private void UpdateControlsVisibility()
  608. {
  609. if (UserInteraction.IsUserInputThisFrame() || !CanHideControls())
  610. {
  611. UserInteraction.InactiveTime = 0f;
  612. FadeUpControls();
  613. }
  614. else
  615. {
  616. UserInteraction.InactiveTime += Time.unscaledDeltaTime;
  617. if (UserInteraction.InactiveTime >= _userInactiveDuration)
  618. {
  619. FadeDownControls();
  620. }
  621. else
  622. {
  623. FadeUpControls();
  624. }
  625. }
  626. }
  627. private void FadeUpControls()
  628. {
  629. if (!_controlsGroup.gameObject.activeSelf)
  630. {
  631. _controlsGroup.gameObject.SetActive(true);
  632. }
  633. _controlsFade = Mathf.Min(1f, _controlsFade + Time.deltaTime * 8f);
  634. _controlsGroup.alpha = Mathf.Pow(_controlsFade, 5f);
  635. }
  636. private void FadeDownControls()
  637. {
  638. if (_controlsGroup.gameObject.activeSelf)
  639. {
  640. _controlsFade = Mathf.Max(0f, _controlsFade - Time.deltaTime * 3f);
  641. _controlsGroup.alpha = Mathf.Pow(_controlsFade, 5f);
  642. if (_controlsGroup.alpha <= 0f)
  643. {
  644. _controlsGroup.gameObject.SetActive(false);
  645. }
  646. }
  647. }
  648. void Update()
  649. {
  650. if (!_mediaPlayer) return;
  651. UpdateControlsVisibility();
  652. UpdateAudioFading();
  653. UpdateAudioSpectrum();
  654. if (_mediaPlayer.Info != null)
  655. {
  656. TimeRange timelineRange = GetTimelineRange();
  657. // Update timeline hover popup
  658. #if (!ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER)
  659. if (_timelineTip != null)
  660. {
  661. if (_isHoveringOverTimeline)
  662. {
  663. Vector2 canvasPos;
  664. RectTransformUtility.ScreenPointToLocalPointInRectangle(_canvasTransform, Input.mousePosition, null, out canvasPos);
  665. _segmentsSeek.gameObject.SetActive(true);
  666. _timelineTip.gameObject.SetActive(true);
  667. Vector3 mousePos = _canvasTransform.TransformPoint(canvasPos);
  668. _timelineTip.position = new Vector2(mousePos.x, _timelineTip.position.y);
  669. if (UserInteraction.IsUserInputThisFrame())
  670. {
  671. // Work out position on the timeline
  672. Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(this._sliderTime.GetComponent<RectTransform>());
  673. float x = Mathf.Clamp01((canvasPos.x - bounds.min.x) / bounds.size.x);
  674. double time = (double)x * timelineRange.Duration;
  675. // Seek to the new position
  676. if (_thumbnailMediaPlayer != null && _thumbnailMediaPlayer.Control != null)
  677. {
  678. _thumbnailMediaPlayer.Control.SeekFast(time);
  679. }
  680. // Update time text
  681. Text hoverText = _timelineTip.GetComponentInChildren<Text>();
  682. if (hoverText != null)
  683. {
  684. time -= timelineRange.startTime;
  685. time = System.Math.Max(time, 0.0);
  686. time = System.Math.Min(time, timelineRange.Duration);
  687. hoverText.text = Helper.GetTimeString(time, false);
  688. }
  689. {
  690. // Update seek segment when hovering over timeline
  691. if (_segmentsSeek != null)
  692. {
  693. float[] ranges = new float[2];
  694. if (timelineRange.Duration > 0.0)
  695. {
  696. double t = ((_mediaPlayer.Control.GetCurrentTime() - timelineRange.startTime) / timelineRange.duration);
  697. ranges[1] = x;
  698. ranges[0] = (float)t;
  699. }
  700. _segmentsSeek.Segments = ranges;
  701. }
  702. }
  703. }
  704. }
  705. else
  706. {
  707. _timelineTip.gameObject.SetActive(false);
  708. _segmentsSeek.gameObject.SetActive(false);
  709. }
  710. }
  711. #endif
  712. // Updated stalled display
  713. if (_overlayManager)
  714. {
  715. _overlayManager.Reset();
  716. if (_mediaPlayer.Info.IsPlaybackStalled())
  717. {
  718. _overlayManager.TriggerStalled();
  719. }
  720. }
  721. // Update keyboard input
  722. if (_enableKeyboardControls)
  723. {
  724. #if (!ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER)
  725. // Keyboard toggle play/pause
  726. if (Input.GetKeyDown(KeyTogglePlayPause))
  727. {
  728. TogglePlayPause();
  729. }
  730. // Keyboard seek 5 seconds
  731. if (Input.GetKeyDown(KeyJumpBack))
  732. {
  733. SeekRelative(-_jumpDeltaTime);
  734. }
  735. else if (Input.GetKeyDown(KeyJumpForward))
  736. {
  737. SeekRelative(_jumpDeltaTime);
  738. }
  739. // Keyboard control volume
  740. if (Input.GetKeyDown(KeyVolumeUp))
  741. {
  742. ChangeAudioVolume(_keyVolumeDelta);
  743. }
  744. else if (Input.GetKeyDown(KeyVolumeDown))
  745. {
  746. ChangeAudioVolume(-_keyVolumeDelta);
  747. }
  748. // Keyboard toggle mute
  749. if (Input.GetKeyDown(KeyToggleMute))
  750. {
  751. ToggleMute();
  752. }
  753. #endif
  754. }
  755. // Animation play/pause button
  756. if (_playPauseMaterial != null)
  757. {
  758. float t = _playPauseMaterial.GetFloat(_propMorph.Id);
  759. float d = 1f;
  760. if (_mediaPlayer.Control.IsPlaying())
  761. {
  762. d = -1f;
  763. }
  764. t += d * Time.deltaTime * 6f;
  765. t = Mathf.Clamp01(t);
  766. _playPauseMaterial.SetFloat(_propMorph.Id, t);
  767. }
  768. // Animation volume/mute button
  769. if (_volumeMaterial != null)
  770. {
  771. float t = _volumeMaterial.GetFloat(_propMute.Id);
  772. float d = 1f;
  773. if (!_mediaPlayer.AudioMuted)
  774. {
  775. d = -1f;
  776. }
  777. t += d * Time.deltaTime * 6f;
  778. t = Mathf.Clamp01(t);
  779. _volumeMaterial.SetFloat(_propMute.Id, t);
  780. _volumeMaterial.SetFloat(_propVolume.Id, _audioVolume);
  781. }
  782. // Animation subtitles button
  783. if (_subtitlesMaterial)
  784. {
  785. float t = _subtitlesMaterial.GetFloat(_propMorph.Id);
  786. float d = 1f;
  787. if (_mediaPlayer.TextTracks.GetActiveTextTrack() == null)
  788. {
  789. d = -1f;
  790. }
  791. t += d * Time.deltaTime * 6f;
  792. t = Mathf.Clamp01(t);
  793. _subtitlesMaterial.SetFloat(_propMorph.Id, t);
  794. }
  795. // Animation options button
  796. if (_optionsMaterial)
  797. {
  798. float t = _optionsMaterial.GetFloat(_propMorph.Id);
  799. float d = 1f;
  800. if (!_showOptions)
  801. {
  802. d = -1f;
  803. }
  804. t += d * Time.deltaTime * 6f;
  805. t = Mathf.Clamp01(t);
  806. _optionsMaterial.SetFloat(_propMorph.Id, t);
  807. }
  808. // Update time/duration text display
  809. if (_textTimeDuration)
  810. {
  811. string t1 = Helper.GetTimeString((_mediaPlayer.Control.GetCurrentTime() - timelineRange.startTime), false);
  812. string d1 = Helper.GetTimeString(timelineRange.duration, false);
  813. _textTimeDuration.text = string.Format("{0} / {1}", t1, d1);
  814. }
  815. // Update volume slider
  816. if (!_useAudioFading)
  817. {
  818. UpdateVolumeSlider();
  819. }
  820. // Update time slider position
  821. if (_sliderTime && !_isHoveringOverTimeline)
  822. {
  823. double t = 0.0;
  824. if (timelineRange.duration > 0.0)
  825. {
  826. t = ((_mediaPlayer.Control.GetCurrentTime() - timelineRange.startTime) / timelineRange.duration);
  827. }
  828. _sliderTime.value = Mathf.Clamp01((float)t);
  829. }
  830. // Update LIVE text visible
  831. if (_liveItem)
  832. {
  833. _liveItem.SetActive(double.IsInfinity(_mediaPlayer.Info.GetDuration()));
  834. }
  835. // Update subtitle button visible
  836. if (_buttonSubtitles)
  837. {
  838. _buttonSubtitles.gameObject.SetActive(_mediaPlayer.TextTracks.GetTextTracks().Count > 0);
  839. }
  840. // Update media name
  841. if (_textMediaName)
  842. {
  843. #if MEDIA_NAME
  844. string mediaName = string.Empty;
  845. if (!string.IsNullOrEmpty(_mediaPlayer.VideoPath))
  846. {
  847. mediaName = System.IO.Path.GetFileName(_mediaPlayer.VideoPath);
  848. if (mediaName.Length > 26)
  849. {
  850. mediaName = mediaName.Substring(0, 26);
  851. }
  852. }
  853. #endif
  854. string resolutionName = string.Empty;
  855. if (_mediaPlayer.Info.GetVideoWidth() > 0)
  856. {
  857. resolutionName = Helper.GetFriendlyResolutionName(_mediaPlayer.Info.GetVideoWidth(), _mediaPlayer.Info.GetVideoHeight(), _mediaPlayer.Info.GetVideoFrameRate());
  858. }
  859. #if MEDIA_NAME
  860. _textMediaName.text = string.Format("{0} {1}", mediaName, resolutionName);
  861. #else
  862. _textMediaName.text = resolutionName;
  863. #endif
  864. }
  865. // Update buffered segments
  866. if (_segmentsBuffered)
  867. {
  868. TimeRanges times = _mediaPlayer.Control.GetBufferedTimes();
  869. float[] ranges = null;
  870. if (times.Count > 0 && timelineRange.duration > 0.0)
  871. {
  872. ranges = new float[times.Count * 2];
  873. for (int i = 0; i < times.Count; i++)
  874. {
  875. ranges[i * 2 + 0] = Mathf.Max(0f, (float)((times[i].StartTime - timelineRange.startTime) / timelineRange.duration));
  876. ranges[i * 2 + 1] = Mathf.Min(1f,(float)((times[i].EndTime - timelineRange.startTime) / timelineRange.duration));
  877. }
  878. }
  879. _segmentsBuffered.Segments = ranges;
  880. }
  881. // Update progress segment
  882. if (_segmentsProgress)
  883. {
  884. TimeRanges times = _mediaPlayer.Control.GetBufferedTimes();
  885. float[] ranges = null;
  886. if (times.Count > 0 && timelineRange.Duration > 0.0)
  887. {
  888. ranges = new float[2];
  889. double x1 = (times.MinTime - timelineRange.startTime) / timelineRange.duration;
  890. double x2 = ((_mediaPlayer.Control.GetCurrentTime() - timelineRange.startTime) / timelineRange.duration);
  891. ranges[0] = Mathf.Max(0f, (float)x1);
  892. ranges[1] = Mathf.Min(1f, (float)x2);
  893. }
  894. _segmentsProgress.Segments = ranges;
  895. }
  896. }
  897. }
  898. void OnGUI()
  899. {
  900. // NOTE: These this IMGUI is just temporary until we implement the UI using uGUI
  901. if (!_showOptions) return;
  902. if (!_mediaPlayer || _mediaPlayer.Control == null) return;
  903. GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(2f, 2f, 1f));
  904. GUI.backgroundColor = Color.red;
  905. GUILayout.BeginVertical(GUI.skin.box);
  906. GUI.backgroundColor = Color.white;
  907. GUILayout.Label("Duration " + _mediaPlayer.Info.GetDuration() + "s");
  908. GUILayout.BeginHorizontal();
  909. GUILayout.Label("States: ");
  910. GUILayout.Toggle(_mediaPlayer.Control.HasMetaData(), "HasMetaData", GUI.skin.button);
  911. GUILayout.Toggle(_mediaPlayer.Control.IsPaused(), "Paused", GUI.skin.button);
  912. GUILayout.Toggle(_mediaPlayer.Control.IsPlaying(), "Playing", GUI.skin.button);
  913. GUILayout.Toggle(_mediaPlayer.Control.IsBuffering(), "Buffering", GUI.skin.button);
  914. GUILayout.Toggle(_mediaPlayer.Control.IsSeeking(), "Seeking", GUI.skin.button);
  915. GUILayout.Toggle(_mediaPlayer.Control.IsFinished(), "Finished", GUI.skin.button);
  916. GUILayout.EndHorizontal();
  917. {
  918. TimeRanges times = _mediaPlayer.Control.GetBufferedTimes();
  919. if (times != null)
  920. {
  921. GUILayout.Label("Buffered Range " + times.MinTime + " - " + times.MaxTime);
  922. }
  923. }
  924. {
  925. TimeRanges times = _mediaPlayer.Control.GetSeekableTimes();
  926. if (times != null)
  927. {
  928. GUILayout.Label("Seek Range " + times.MinTime + " - " + times.MaxTime);
  929. }
  930. }
  931. {
  932. GUILayout.Label("Video Tracks: " + _mediaPlayer.VideoTracks.GetVideoTracks().Count);
  933. GUILayout.BeginVertical();
  934. VideoTrack selectedTrack = null;
  935. foreach (VideoTrack track in _mediaPlayer.VideoTracks.GetVideoTracks())
  936. {
  937. bool isSelected = (track == _mediaPlayer.VideoTracks.GetActiveVideoTrack());
  938. if (isSelected) GUI.color= Color.green;
  939. if (GUILayout.Button(track.DisplayName, GUILayout.ExpandWidth(false)))
  940. {
  941. selectedTrack = track;
  942. }
  943. if (isSelected) GUI.color= Color.white;
  944. }
  945. GUILayout.EndHorizontal();
  946. if (selectedTrack != null)
  947. {
  948. _mediaPlayer.VideoTracks.SetActiveVideoTrack(selectedTrack);
  949. }
  950. }
  951. {
  952. GUILayout.Label("Audio Tracks: " + _mediaPlayer.AudioTracks.GetAudioTracks().Count);
  953. GUILayout.BeginVertical();
  954. AudioTrack selectedTrack = null;
  955. foreach (AudioTrack track in _mediaPlayer.AudioTracks.GetAudioTracks())
  956. {
  957. bool isSelected = (track == _mediaPlayer.AudioTracks.GetActiveAudioTrack());
  958. if (isSelected) GUI.color= Color.green;
  959. if (GUILayout.Button(track.DisplayName, GUILayout.ExpandWidth(false)))
  960. {
  961. selectedTrack = track;
  962. }
  963. if (isSelected) GUI.color= Color.white;
  964. }
  965. GUILayout.EndHorizontal();
  966. if (selectedTrack != null)
  967. {
  968. _mediaPlayer.AudioTracks.SetActiveAudioTrack(selectedTrack);
  969. }
  970. }
  971. {
  972. GUILayout.Label("Text Tracks: " + _mediaPlayer.TextTracks.GetTextTracks().Count);
  973. GUILayout.BeginVertical();
  974. TextTrack selectedTrack = null;
  975. foreach (TextTrack track in _mediaPlayer.TextTracks.GetTextTracks())
  976. {
  977. bool isSelected = (track == _mediaPlayer.TextTracks.GetActiveTextTrack());
  978. if (isSelected) GUI.color= Color.green;
  979. if (GUILayout.Button(track.DisplayName, GUILayout.ExpandWidth(false)))
  980. {
  981. selectedTrack = track;
  982. }
  983. if (isSelected) GUI.color= Color.white;
  984. }
  985. GUILayout.EndHorizontal();
  986. if (selectedTrack != null)
  987. {
  988. _mediaPlayer.TextTracks.SetActiveTextTrack(selectedTrack);
  989. }
  990. }
  991. {
  992. GUILayout.Label("FPS: " + _mediaPlayer.Info.GetVideoDisplayRate().ToString("F2"));
  993. }
  994. #if (UNITY_STANDALONE_WIN)
  995. if (_mediaPlayer.PlatformOptionsWindows.bufferedFrameSelection != BufferedFrameSelectionMode.None)
  996. {
  997. IBufferedDisplay bufferedDisplay = _mediaPlayer.BufferedDisplay;
  998. if (bufferedDisplay != null)
  999. {
  1000. BufferedFramesState state = bufferedDisplay.GetBufferedFramesState();
  1001. GUILayout.BeginHorizontal();
  1002. GUILayout.Label("Buffered Frames: " + state.bufferedFrameCount);
  1003. GUILayout.HorizontalSlider(state.bufferedFrameCount, 0f, 12f);
  1004. GUILayout.EndHorizontal();
  1005. GUILayout.BeginHorizontal();
  1006. GUILayout.Label("Free Frames: " + state.freeFrameCount);
  1007. GUILayout.HorizontalSlider(state.freeFrameCount, 0f, 12f);
  1008. GUILayout.EndHorizontal();
  1009. GUILayout.Label("Min Timstamp: " + state.minTimeStamp);
  1010. GUILayout.Label("Max Timstamp: " + state.maxTimeStamp);
  1011. GUILayout.Label("Display Timstamp: " + _mediaPlayer.TextureProducer.GetTextureTimeStamp());
  1012. }
  1013. }
  1014. #endif
  1015. GUILayout.EndVertical();
  1016. }
  1017. }
  1018. }
  1019. #endif