当前位置:   article > 正文

Unity VideoPlayer播放视频 Slider控制播放进度_unity video player slider

unity video player slider
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Video;
  4. public class ToPlayVideo : MonoBehaviour
  5. {
  6. public VideoClip[] videoClips; // 视频的文件 参数
  7. public Text videoTimeText; // 视频的当前时间 Text
  8. public Text videoNameText; // 视频的总时长 Text
  9. public Slider videoTimeSlider; // 视频的时间 Slider
  10. // 定义参数获取VideoPlayer组件和RawImage组件
  11. internal VideoPlayer videoPlayer;
  12. private RawImage rawImage;
  13. public Texture pingBao; // 屏保图片
  14. // 当前视频的总时间值和当前播放时间值的参数
  15. private int currentHour;
  16. private int currentMinute;
  17. private int currentSecond;
  18. private int clipHour;
  19. private int clipMinute;
  20. private int clipSecond;
  21. public Sprite play;
  22. public Sprite pause;
  23. private bool isPlay = true; //是否正在播放视频
  24. public Button playPauseButton; //播放暂停按钮
  25. public Button stopButton; //停止播放按钮
  26. public Button button1;
  27. public Button button2;
  28. public Button button3;
  29. public Button button4;
  30. // Use this for initialization
  31. void Start()
  32. {
  33. //清空文本
  34. videoNameText.text = "";
  35. videoTimeText.text = "";
  36. //获取场景中对应的组件
  37. videoPlayer = this.GetComponent<VideoPlayer>();
  38. rawImage = this.GetComponent<RawImage>();
  39. videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
  40. videoPlayer.SetTargetAudioSource(0, this.GetComponent<AudioSource>());
  41. videoPlayer.playOnAwake = false;
  42. videoPlayer.IsAudioTrackEnabled(0);
  43. playPauseButton.onClick.AddListener(PlayOrPause);
  44. stopButton.onClick.AddListener(StopVideo);
  45. button1.onClick.AddListener(delegate { OnClick(0); });
  46. button2.onClick.AddListener(delegate { OnClick(1); });
  47. button3.onClick.AddListener(delegate { OnClick(2); });
  48. button4.onClick.AddListener(delegate { OnClick(3); });
  49. }
  50. // Update is called once per frame
  51. void Update()
  52. {
  53. //如果videoPlayer没有对应的视频texture,则返回屏保
  54. if (videoPlayer.texture == null)
  55. {
  56. rawImage.texture = pingBao;
  57. return;
  58. }
  59. //把VideoPlayerd的视频渲染到UGUI的RawImage
  60. rawImage.texture = videoPlayer.texture;
  61. ShowVideoTime();
  62. }
  63. /// <summary>
  64. /// 显示当前视频的时间
  65. /// </summary>
  66. private void ShowVideoTime()
  67. {
  68. // 当前的视频播放时间
  69. currentHour = (int)videoPlayer.time / 3600;
  70. currentMinute = (int)(videoPlayer.time - currentHour * 3600) / 60;
  71. currentSecond = (int)(videoPlayer.time - currentHour * 3600 - currentMinute * 60);
  72. // 把当前视频播放的时间显示在 Text 上
  73. videoTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2}",
  74. currentHour, currentMinute, currentSecond);
  75. // 把当前视频播放的时间比例赋值到 Slider 上
  76. videoTimeSlider.value = (float)(videoPlayer.time / videoPlayer.clip.length);
  77. }
  78. /// <summary>
  79. /// 显示视频的总时长
  80. /// </summary>
  81. /// <param name="videos">当前视频</param>
  82. void ShowVideoLength(VideoClip videos)
  83. {
  84. videoPlayer.clip = videos;
  85. videoPlayer.Play();
  86. videoTimeSlider.gameObject.SetActive(true);
  87. clipHour = (int)videoPlayer.clip.length / 3600;
  88. clipMinute = (int)(videoPlayer.clip.length - clipHour * 3600) / 60;
  89. clipSecond = (int)(videoPlayer.clip.length - clipHour * 3600 - clipMinute * 60);
  90. videoNameText.text = string.Format("{0:D2}:{1:D2}:{2:D2} ",
  91. clipHour, clipMinute, clipSecond);
  92. }
  93. private void OnClick(int num)
  94. {
  95. switch (num)
  96. {
  97. case 0:
  98. ShowVideoLength(videoClips[0]);
  99. break;
  100. case 1:
  101. ShowVideoLength(videoClips[1]);
  102. break;
  103. case 2:
  104. ShowVideoLength(videoClips[2]);
  105. break;
  106. case 3:
  107. ShowVideoLength(videoClips[3]);
  108. break;
  109. case 4:
  110. ShowVideoLength(videoClips[4]);
  111. break;
  112. default:
  113. break;
  114. }
  115. }
  116. /// <summary>
  117. /// 视频播放暂停
  118. /// </summary>
  119. void PlayOrPause()
  120. {
  121. //如果视频片段不为空,并且视频画面没有播放完毕
  122. if (videoPlayer.clip != null && (ulong)videoPlayer.frame < videoPlayer.frameCount)
  123. {
  124. //如果视频正在播放
  125. if (isPlay)
  126. {
  127. playPauseButton.image.sprite = pause;
  128. videoPlayer.Pause();
  129. isPlay = false;
  130. }
  131. else
  132. {
  133. playPauseButton.image.sprite = play;
  134. videoPlayer.Play();
  135. isPlay = true;
  136. }
  137. }
  138. }
  139. void StopVideo()
  140. {
  141. videoPlayer.Stop();
  142. playPauseButton.image.sprite = play;
  143. videoTimeSlider.gameObject.SetActive(false);
  144. videoNameText.text = "";
  145. videoTimeText.text = "";
  146. isPlay = true;
  147. }
  148. }
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. /// <summary>
  4. /// 继承 拖拽接口
  5. /// </summary>
  6. public class SliderEvent : MonoBehaviour, IDragHandler, IEndDragHandler
  7. {
  8. [SerializeField]
  9. public ToPlayVideo toPlayVideo; // 视频播放的脚本
  10. // Use this for initialization
  11. void Start()
  12. {
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. }
  18. /// <summary>
  19. /// 给 Slider 添加开始拖拽事件
  20. /// </summary>
  21. /// <param name="eventData"></param>
  22. public void OnDrag(PointerEventData eventData)
  23. {
  24. toPlayVideo.videoPlayer.Pause();
  25. SetVideoTimeValueChange();
  26. }
  27. /// <summary>
  28. /// 当前的 Slider 比例值转换为当前的视频播放时间
  29. /// </summary>
  30. private void SetVideoTimeValueChange()
  31. {
  32. toPlayVideo.videoPlayer.time = toPlayVideo.videoTimeSlider.value * toPlayVideo.videoPlayer.clip.length;
  33. }
  34. /// <summary>
  35. /// 给 Slider 添加结束拖拽事件
  36. /// </summary>
  37. /// <param name="eventData"></param>
  38. public void OnEndDrag(PointerEventData eventData)
  39. {
  40. toPlayVideo.videoPlayer.Play();
  41. }
  42. }

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号