RemoteVideoControl.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5. using System;
  6. using UnityEngine.UI;
  7. public class RemoteVideoControl : MonoBehaviour
  8. {
  9. public GameObject Icon;
  10. public GameObject Stop;
  11. bool? isStart = true;
  12. public VideoPlayer videoPlayer;
  13. public Slider slider;
  14. //当前时间
  15. public Text currentTime;
  16. //影片时间
  17. public Text textTotalTime;
  18. //是否获取总时长
  19. private bool IsStart;
  20. //时 分的转换
  21. private int hour, mint;
  22. private float time;
  23. public void VideoStartSwitch()
  24. {
  25. isStart = videoPlayer.isPlaying;
  26. isStart = !isStart;
  27. Icon.SetActive(!isStart.Value);
  28. Stop.SetActive(isStart.Value);
  29. if (isStart.Value)
  30. {
  31. videoPlayer?.Play();
  32. }
  33. else
  34. {
  35. videoPlayer?.Pause();
  36. }
  37. }
  38. private void Start()
  39. {
  40. IsStart = true;
  41. slider.onValueChanged.AddListener(delegate { ChangeVideo(slider.value); });
  42. }
  43. public void ChangeVideo(float value)
  44. {
  45. if (videoPlayer.isPrepared)
  46. {
  47. float tempvalue = max * value;
  48. time = tempvalue;
  49. hour = (int)time / 60;
  50. mint = (int)time % 60;
  51. currentTime.text = string.Format("{0}:{1}", hour.ToString("00"), mint.ToString("00"));
  52. }
  53. if (value > 0.99)
  54. {
  55. Icon.SetActive(true);
  56. Stop.SetActive(false);
  57. }
  58. }
  59. float max;
  60. void Update()
  61. {
  62. if (videoPlayer.isPlaying && IsStart)
  63. {
  64. //帧数/帧速率=总时长 如果是本地直接赋值的视频,我们可以通过VideoClip.length获取总时长
  65. max = videoPlayer.frameCount / videoPlayer.frameRate;
  66. time = max;
  67. hour = (int)time / 60;
  68. mint = (int)time % 60;
  69. // textTotalTime.text = string.Format("/ {0:D2}:{1:D2}", hour.ToString(), mint.ToString());
  70. textTotalTime.text = string.Format("{0}:{1}", hour.ToString("00"), mint.ToString("00"));
  71. IsStart = !IsStart;
  72. }
  73. }
  74. }