当前位置:   article > 正文

【Unity】UGUI 制作音乐播放器_unity自制音乐游戏

unity自制音乐游戏

首先看一下效果


思路:就是算出当前要播放的clip 的长度 换算成时间,显示为这个音频的总时间 10:49

AudioSource.time 就是当前音频播放的时间 进行换算 就是为当前显示的播放时间 00:37

核心代码

  1. public AudioSource s2AudioManger;
  2. public AudioClip audio;
  3. private int clipHour;
  4. private int clipMinute;
  5. private int clipSecond;
  6. private int currentHour;
  7. private int currentMinute;
  8. private int currentSecond;
  9. private string audioTimeText;
  10. public void AudioLength ()
  11. {
  12. clipHour = (int)s2AudioManger.clip.length / 3600;
  13. clipMinute = (int)(s2AudioManger.clip.length - clipHour * 3600) / 60;
  14. clipSecond = (int)(s2AudioManger.clip.length - clipHour * 3600 - clipMinute * 60);
  15. Debug.Log ("clipHour" + clipHour);
  16. Debug.Log ("clipMinute" + clipMinute);
  17. Debug.Log ("clipSecond" + clipSecond);
  18. }
  19. public string ShowAudioTime ()
  20. {
  21. currentHour = (int)s2AudioManger.time / 3600;
  22. currentMinute = (int)(s2AudioManger.time - currentHour * 3600) / 60;
  23. currentSecond = (int)(s2AudioManger.time - currentHour * 3600 - currentMinute * 60);
  24. audioTimeText = string.Format ("{0:D2}:{1:D2}:{2:D2}/{3:D2}:{4:D2}:{5:D2}", currentHour, currentMinute, currentSecond, clipHour, clipMinute, clipSecond);
  25. return audioTimeText;
  26. }
  27. // 当前Slider 进度显示
  28. public float MusicSlider ()
  29. {
  30. return s2AudioManger.time / s2AudioManger.clip.length;
  31. }



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

闽ICP备14008679号