RemoteVideoControl.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. }
  37. else
  38. {
  39. // videoPlayer?.Pause();
  40. avProVideoPlayer?.Pause();
  41. }
  42. }
  43. private void Start()
  44. {
  45. IsStart = true;
  46. slider.onValueChanged.AddListener(delegate { ChangeVideo(slider.value); });
  47. StartCoroutine(VideoTimer());
  48. }
  49. public void ChangeVideo(float value)
  50. {
  51. //if (avProVideoPlayer.IsVideoReady())
  52. //{
  53. // float tempvalue = max * value;
  54. // time = tempvalue;
  55. // hour = (int)time / 60;
  56. // mint = (int)time % 60;
  57. // currentTime.text = string.Format("{0}:{1}", hour.ToString("00"), mint.ToString("00"));
  58. //}
  59. }
  60. float max;
  61. private IEnumerator VideoState()
  62. {
  63. while(true)
  64. {
  65. yield return new WaitForSeconds(0.1f);
  66. switch (avProVideoPlayer.GetVideoState())
  67. {
  68. case MediaPlayerCtrl.MEDIAPLAYER_STATE.NOT_READY:
  69. if (!Icon.activeSelf)
  70. {
  71. Icon.SetActive(true);
  72. Stop.SetActive(false);
  73. }
  74. break;
  75. case MediaPlayerCtrl.MEDIAPLAYER_STATE.READY:
  76. break;
  77. case MediaPlayerCtrl.MEDIAPLAYER_STATE.END:
  78. if (!Icon.activeSelf)
  79. {
  80. Icon.SetActive(true);
  81. Stop.SetActive(false);
  82. }
  83. break;
  84. case MediaPlayerCtrl.MEDIAPLAYER_STATE.PLAYING:
  85. if (Icon.activeSelf)
  86. {
  87. Icon.SetActive(false);
  88. Stop.SetActive(true);
  89. }
  90. break;
  91. case MediaPlayerCtrl.MEDIAPLAYER_STATE.PAUSED:
  92. if (!Icon.activeSelf)
  93. {
  94. Icon.SetActive(true);
  95. Stop.SetActive(false);
  96. }
  97. break;
  98. case MediaPlayerCtrl.MEDIAPLAYER_STATE.STOPPED:
  99. if (!Icon.activeSelf)
  100. {
  101. Icon.SetActive(true);
  102. Stop.SetActive(false);
  103. }
  104. break;
  105. case MediaPlayerCtrl.MEDIAPLAYER_STATE.ERROR:
  106. if (!Icon.activeSelf)
  107. {
  108. Icon.SetActive(true);
  109. Stop.SetActive(false);
  110. }
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. }
  117. private IEnumerator VideoTimer()
  118. {
  119. while (true)
  120. {
  121. yield return new WaitForSeconds(0.1f);
  122. if (avProVideoPlayer.IsVideoReady()&& !AVideoSlide.isDown)
  123. {
  124. float maxTimer = avProVideoPlayer.GetMaxTimer();
  125. float nowTimer = avProVideoPlayer.GetNowTimer();
  126. textTotalTime.text = string.Format("{0}:{1}", (maxTimer / 60).ToString("00"), (maxTimer % 60).ToString("00"));
  127. currentTime.text = string.Format("{0}:{1}", (nowTimer / 60).ToString("00"), (nowTimer % 60).ToString("00"));
  128. slider.value = maxTimer / nowTimer;
  129. //if (textTotalTime.text == currentTime.text)
  130. //{
  131. // Icon.SetActive(true);
  132. // Stop.SetActive(false);
  133. //}
  134. }
  135. }
  136. }
  137. //void Update()
  138. //{
  139. // if (IsStart && avProVideoPlayer.IsVideoReady())
  140. // {
  141. // //max = avProVideoPlayer.GetMaxTimer();
  142. // //time = max;
  143. // //hour = (int)time / 60;
  144. // //mint = (int)time % 60;
  145. // // textTotalTime.text = string.Format("/ {0:D2}:{1:D2}", hour.ToString(), mint.ToString());
  146. // textTotalTime.text = string.Format("{0}:{1}",avProVideoPlayer.GetNowTimer().ToString("00"), avProVideoPlayer.GetMaxTimer().ToString("00"));
  147. // IsStart = !IsStart;
  148. // }
  149. //}
  150. }