当前位置:   article > 正文

Unity VideoPlayer 滑动条控制播放进度,滑动过程可显示滑动画面_unity videoplayer滑动条滚动

unity videoplayer滑动条滚动

unity的VideoPlayer其实足够了,不用插件也可以很好的干活。

按照我的步骤来一趟即可。本文主要给一个可以实现滑动的基础逻辑,可在此基础上做扩展。

先看看效果

代码块:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Video;
  7. /// <summary>
  8. /// 滑动条控制视频播放进度,可静帧或者滑动播放
  9. /// </summary>
  10. public class PlayVideo : MonoBehaviour
  11. {
  12. public VideoClip videoClip; // 视频的文件 参数
  13. public MySlider videoTimeSlider; // 视频的时间 Slider
  14. //定义参数获取VideoPlayer组件和RawImage组件
  15. internal VideoPlayer videoPlayer;
  16. private RawImage rawImage;
  17. // Use this for initialization
  18. void Start()
  19. {
  20. //获取场景中对应的组件
  21. videoPlayer = this.GetComponent<VideoPlayer>();
  22. rawImage = this.GetComponent<RawImage>();
  23. videoPlayer.clip = videoClip;
  24. //videoNameText.text = videoClip.name;
  25. clipHour = (int)videoPlayer.clip.length / 3600;
  26. clipMinute = (int)(videoPlayer.clip.length - clipHour * 3600) / 60;
  27. clipSecond = (int)(videoPlayer.clip.length - clipHour * 3600 - clipMinute * 60);
  28. videoPlayer.Play();
  29. videoTimeSlider.OnChangeValue += OnChangeValue;
  30. videoPlayer.sendFrameReadyEvents = true;//激活frameReady的必要选项
  31. videoPlayer.frameReady += VideoPlayer_frameReady;
  32. }
  33. private bool _isReady = false;
  34. private bool _isDrag = false;
  35. private void VideoPlayer_frameReady(VideoPlayer source, long frameIdx)
  36. {
  37. _isReady = true;
  38. }
  39. private void OnChangeValue(float value,bool isDrag)
  40. {
  41. _isReady = false;
  42. _isDrag = isDrag;
  43. float oldVal;
  44. if (isDrag)
  45. {
  46. oldVal = (float)videoPlayer.time / (float)videoPlayer.clip.length;
  47. // Debug.Log("oldVal is:" + oldVal + " value is:" + value);
  48. if (Math.Abs(oldVal - value) < 0.055f) return;
  49. }
  50. videoPlayer.time = value * (float)videoPlayer.clip.length;
  51. //以下两种方法可二选一
  52. //滑动的时候定帧
  53. if (_isDrag)
  54. videoPlayer.Pause();
  55. else videoPlayer.Play();
  56. //滑动的时候,自动播放
  57. //videoPlayer.Play();
  58. }
  59. // Update is called once per frame
  60. void Update()
  61. {
  62. if (videoPlayer.texture == null)
  63. {
  64. return;
  65. }
  66. if(videoPlayer.texture!=rawImage.texture)
  67. rawImage.texture = videoPlayer.texture;
  68. ShowVideoTime();
  69. }
  70. /// <summary>
  71. /// 显示当前视频的时间
  72. /// </summary>
  73. private void ShowVideoTime()
  74. {
  75. // 当前的视频播放时间
  76. currentHour = (int)videoPlayer.time / 3600;
  77. currentMinute = (int)(videoPlayer.time - currentHour * 3600) / 60;
  78. currentSecond = (int)(videoPlayer.time - currentHour * 3600 - currentMinute * 60);
  79. // 把当前视频播放的时间显示在 Text 上
  80. //videoTimeText.text = string.Format("{0:D2}:{1:D2}:{2:D2} / {3:D2}:{4:D2}:{5:D2}", currentHour, currentMinute, currentSecond, clipHour, clipMinute, clipSecond);
  81. // 把当前视频播放的时间比例赋值到 Slider 上
  82. if (_isReady && !_isDrag)
  83. {
  84. videoTimeSlider.SetValue((float)(videoPlayer.time / videoPlayer.clip.length));
  85. }
  86. }
  87. // 当前视频的总时间值和当前播放时间值的参数
  88. private int currentHour;
  89. private int currentMinute;
  90. private int currentSecond;
  91. private int clipHour;
  92. private int clipMinute;
  93. private int clipSecond;
  94. }
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// 滑动位置,预览该位置的视频定帧
  8. /// </summary>
  9. public class MySlider : Slider
  10. {
  11. public Action<float,bool> OnChangeValue;
  12. private bool _isDrag = false;
  13. private Coroutine _coroutine;
  14. private WaitForSeconds _waitForSeconds;
  15. protected override void Awake()
  16. {
  17. base.Awake();
  18. _waitForSeconds=new WaitForSeconds(0.2f);
  19. }
  20. public override void OnPointerDown(PointerEventData eventData)
  21. {
  22. base.OnPointerUp(eventData);
  23. _coroutine = StartCoroutine(Wait());
  24. _isDrag = true;
  25. }
  26. public override void OnPointerUp(PointerEventData eventData)
  27. {
  28. base.OnPointerUp(eventData);
  29. _isDrag = false;
  30. if (_coroutine != null) StopCoroutine(_coroutine);
  31. _coroutine = null;
  32. if (OnChangeValue != null)
  33. OnChangeValue(this.value, _isDrag);
  34. }
  35. public void SetValue(float newValue)
  36. {
  37. if (_isDrag) return;
  38. this.value = newValue;
  39. }
  40. private IEnumerator Wait()
  41. {
  42. while (true)
  43. {
  44. yield return _waitForSeconds;
  45. if (OnChangeValue != null)
  46. OnChangeValue(this.value, _isDrag);
  47. }
  48. }
  49. }

步骤一:

 步骤二:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/138243
推荐阅读
相关标签
  

闽ICP备14008679号