当前位置:   article > 正文

unity VideoPlayer的使用_unity 如何计算出videoplayer url视频当前播放的时间

unity 如何计算出videoplayer url视频当前播放的时间

         一直想做一个切换进度的功能,因为MovieTextureAPI过少,一直有这个遗憾,unity5.6之后加了一个videoPlayer组件,就想做一个,由于接触unity时间也不长,也踩了很多坑,不管怎么说,还是做了出来。不多说,上代码:

  1. using UnityEngine;
  2. using UnityEngine.Video;
  3. public static class VideoController
  4. {
  5. /// <summary>
  6. /// 获取视频总时长
  7. /// </summary>
  8. /// <param name="vsp"></param>
  9. /// <returns></returns>
  10. public static int GetVideoTimeCount(this VideoPlayer vp)
  11. {
  12. return (int)(vp.frameCount / vp.frameRate);
  13. }
  14. /// <summary>
  15. /// 获取视频进度
  16. /// </summary>
  17. /// <param name="vsp"></param>
  18. /// <returns></returns>
  19. public static float GetVideoProgression(this VideoPlayer vp)
  20. {
  21. return (float)((vp.time * vp.frameRate)/(vp.frameCount / vp.frameRate));
  22. }
  23. /// <summary>
  24. /// 设置视频进度
  25. /// </summary>
  26. /// <param name="vp"></param>
  27. /// <param name="progression"></param>
  28. public static void SetVideoProgression(this VideoPlayer vp, float progression)
  29. {
  30. float time = (int)vp.frameCount / vp.frameRate * progression;
  31. vp.time = time;
  32. vp.Play();
  33. }
  34. }

 

 

 

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.Video;
  7. public class VideoTest : MonoBehaviour
  8. {
  9. public VideoPlayer vp;
  10. public Slider progression;
  11. public Text timeCount;
  12. public Text currentTime;
  13. void Start()
  14. {
  15. vp.Play();
  16. progression.value = vp.GetVideoProgression();
  17. progression.onValueChanged.AddListener(Changed);
  18. DateFormat((int)vp.GetVideoTimeCount(), timeCount);
  19. }
  20. private void DateFormat(int sec, Text text)
  21. {
  22. TimeSpan span = new TimeSpan(0, 0, 0, sec);
  23. text.text = (int)span.Hours + ":" + (int)span.Minutes + ":" + (int)span.Seconds;
  24. }
  25. // Update is called once per frame
  26. void Update()
  27. {
  28. DateFormat((int)vp.time, currentTime);
  29. }
  30. private void Changed(float value)
  31. {
  32. vp.SetVideoProgression(value);
  33. }
  34. public void Play()
  35. {
  36. vp.Play();
  37. }
  38. public void Pause()
  39. {
  40. vp.Pause();
  41. }
  42. }

 

 

效果图如下:   

 

 

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

闽ICP备14008679号