RemoteVideoControl.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5. using System;
  6. using UnityEngine.UI;
  7. using TMPro;
  8. public class RemoteVideoControl : MonoBehaviour
  9. {
  10. public GameObject Icon;
  11. public GameObject Stop;
  12. bool? isStart = true;
  13. // public VideoPlayer videoPlayer;
  14. public AVProVideoPlayer avProVideoPlayer;
  15. public Slider slider;
  16. //当前时间
  17. public Text currentTime;
  18. //影片时间
  19. public Text textTotalTime;
  20. //是否获取总时长
  21. private bool IsStart;
  22. //时 分的转换
  23. private int hour, mint;
  24. private float time;
  25. public void VideoStartSwitch()
  26. {
  27. // isStart = videoPlayer.isPlaying;
  28. isStart = avProVideoPlayer.IsPlaying();
  29. isStart = !isStart;
  30. //Icon.SetActive(!isStart.Value);
  31. //Stop.SetActive(isStart.Value);
  32. if (isStart.Value)
  33. {
  34. // videoPlayer?.Play();
  35. avProVideoPlayer?.Play();
  36. // slider.value = 0;
  37. }
  38. else
  39. {
  40. // videoPlayer?.Pause();
  41. avProVideoPlayer?.Pause();
  42. }
  43. }
  44. private void Start()
  45. {
  46. IsStart = true;
  47. slider.onValueChanged.AddListener(delegate { ChangeVideo(slider.value); });
  48. StartCoroutine(VideoTimer());
  49. StartCoroutine(VideoState());
  50. }
  51. public void ChangeVideo(float value)
  52. {
  53. //if (avProVideoPlayer.IsVideoReady())
  54. //{
  55. // float tempvalue = max * value;
  56. // time = tempvalue;
  57. // hour = (int)time / 60;
  58. // mint = (int)time % 60;
  59. // currentTime.text = string.Format("{0}:{1}", hour.ToString("00"), mint.ToString("00"));
  60. //}
  61. }
  62. float max;
  63. private IEnumerator VideoState()
  64. {
  65. while(true)
  66. {
  67. yield return new WaitForSeconds(0.1f);
  68. switch (avProVideoPlayer.mediaPlayer.m_CurrentState)
  69. {
  70. case MediaPlayerCtrl.MEDIAPLAYER_STATE.NOT_READY:
  71. if (!Icon.activeSelf)
  72. {
  73. Icon.SetActive(true);
  74. Stop.SetActive(false);
  75. }
  76. break;
  77. case MediaPlayerCtrl.MEDIAPLAYER_STATE.READY:
  78. break;
  79. case MediaPlayerCtrl.MEDIAPLAYER_STATE.END:
  80. if (!Icon.activeSelf)
  81. {
  82. Icon.SetActive(true);
  83. Stop.SetActive(false);
  84. }
  85. break;
  86. case MediaPlayerCtrl.MEDIAPLAYER_STATE.PLAYING:
  87. if (Icon.activeSelf)
  88. {
  89. Icon.SetActive(false);
  90. Stop.SetActive(true);
  91. }
  92. break;
  93. case MediaPlayerCtrl.MEDIAPLAYER_STATE.PAUSED:
  94. if (!Icon.activeSelf)
  95. {
  96. Icon.SetActive(true);
  97. Stop.SetActive(false);
  98. }
  99. break;
  100. case MediaPlayerCtrl.MEDIAPLAYER_STATE.STOPPED:
  101. if (!Icon.activeSelf)
  102. {
  103. Icon.SetActive(true);
  104. Stop.SetActive(false);
  105. }
  106. break;
  107. case MediaPlayerCtrl.MEDIAPLAYER_STATE.ERROR:
  108. if (!Icon.activeSelf)
  109. {
  110. Icon.SetActive(true);
  111. Stop.SetActive(false);
  112. }
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. }
  119. private IEnumerator VideoTimer()
  120. {
  121. while (true)
  122. {
  123. yield return new WaitForSeconds(0.5f);
  124. if (avProVideoPlayer.IsVideoReady()&& !AVideoSlide.isDown)
  125. {
  126. float maxTimer = avProVideoPlayer.GetMaxTimer();
  127. float nowTimer = avProVideoPlayer.GetNowTimer();
  128. textTotalTime.text = string.Format("{0}:{1}", (maxTimer / 60).ToString("00"), (maxTimer % 60).ToString("00"));
  129. currentTime.text = string.Format("{0}:{1}", (nowTimer / 60).ToString("00"), (nowTimer % 60).ToString("00"));
  130. // if(slider.value!=1)
  131. float value = nowTimer / maxTimer;
  132. if (value == float.NaN|| value.ToString() =="NaN")
  133. value = 0.1f;
  134. else if( slider.value != value)
  135. slider.value = value;
  136. //if (textTotalTime.text == currentTime.text)
  137. //{
  138. // Icon.SetActive(true);
  139. // Stop.SetActive(false);
  140. //}
  141. }
  142. }
  143. }
  144. //void Update()
  145. //{
  146. // if (IsStart && avProVideoPlayer.IsVideoReady())
  147. // {
  148. // //max = avProVideoPlayer.GetMaxTimer();
  149. // //time = max;
  150. // //hour = (int)time / 60;
  151. // //mint = (int)time % 60;
  152. // // textTotalTime.text = string.Format("/ {0:D2}:{1:D2}", hour.ToString(), mint.ToString());
  153. // textTotalTime.text = string.Format("{0}:{1}",avProVideoPlayer.GetNowTimer().ToString("00"), avProVideoPlayer.GetMaxTimer().ToString("00"));
  154. // IsStart = !IsStart;
  155. // }
  156. //}
  157. }