RemoteVideoControl.cs 5.0 KB

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