当前位置:   article > 正文

unity 中音乐播放器_unity musicplayer

unity musicplayer

音乐管理器,配置好对应组件

  1. using TMPro;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. public class MusicPlayer : MonoBehaviour
  5. {
  6. private AudioSource audioSource;
  7. public AudioClip[] songs;
  8. private int currentSongIndex = 0;
  9. public TextMeshPro songNameText;// 引用UI文本组件
  10. [Header("ui组件")]
  11. public Text totalTimeText;
  12. public Text currentTimeText;
  13. public Slider timeSlider;
  14. private float totalDuration;
  15. private bool isPlay;
  16. private void Start()
  17. {
  18. audioSource = GetComponent<AudioSource>();
  19. PlayCurrentSong();
  20. timeSlider.onValueChanged.AddListener(OnSliderValueChanged);
  21. UpdateSongTimeText();
  22. }
  23. public void SwitchAudioPlayState()
  24. {
  25. if(isPlay)
  26. {
  27. PlayCurrentSong();
  28. }
  29. else
  30. {
  31. PauseMusic();
  32. }
  33. isPlay=!isPlay;
  34. }
  35. public void UpdateSongTimeText()
  36. {
  37. // 设置滑动条的最大值为1,因为时间范围为01
  38. timeSlider.maxValue = 1f;
  39. // 获取歌曲总时长
  40. totalDuration = audioSource.clip.length;
  41. // 更新总时长文本
  42. totalTimeText.text = FormatTime(totalDuration);
  43. }
  44. public void PlayCurrentSong()
  45. {
  46. if (audioSource != null && songs.Length > 0)
  47. {
  48. audioSource.clip = songs[currentSongIndex];
  49. audioSource.Play();
  50. UpdateSongNameText(); // 更新UI文本显示
  51. }
  52. }
  53. public void PauseMusic()
  54. {
  55. if (audioSource != null && audioSource.isPlaying)
  56. {
  57. audioSource.Pause();
  58. }
  59. }
  60. public void StopMusic()
  61. {
  62. if (audioSource != null && audioSource.isPlaying)
  63. {
  64. audioSource.Stop();
  65. }
  66. }
  67. public void NextSong()
  68. {
  69. currentSongIndex++;
  70. if (currentSongIndex >= songs.Length)
  71. {
  72. currentSongIndex = 0;
  73. }
  74. audioSource.time=0;
  75. PlayCurrentSong();
  76. UpdateSongTimeText();
  77. }
  78. public void PreviousSong()
  79. {
  80. currentSongIndex--;
  81. if (currentSongIndex < 0)
  82. {
  83. currentSongIndex = songs.Length - 1;
  84. }
  85. audioSource.time=0;
  86. PlayCurrentSong();
  87. UpdateSongTimeText();
  88. }
  89. public void FastForward(float seconds)
  90. {
  91. if (audioSource != null && audioSource.isPlaying)
  92. {
  93. audioSource.time += seconds;
  94. }
  95. }
  96. public void SlowForward(float seconds)
  97. {
  98. if (audioSource != null && audioSource.isPlaying)
  99. {
  100. audioSource.time -= seconds;
  101. }
  102. }
  103. private void UpdateSongNameText()
  104. {
  105. if (songNameText != null)
  106. {
  107. songNameText.text = songs[currentSongIndex].name;
  108. }
  109. }
  110. private void Update()
  111. {
  112. // 更新当前进行时间
  113. float currentTime = audioSource.time;
  114. currentTimeText.text = FormatTime(currentTime);
  115. // 更新滑动条的值
  116. timeSlider.value = currentTime / totalDuration;
  117. if (!audioSource.isPlaying&&!isPlay )
  118. {
  119. NextSong();
  120. }
  121. }
  122. public void OnSliderValueChanged(float value)
  123. {
  124. // 将滑动条的值映射到歌曲的时间范围内
  125. float targetTime = value * totalDuration;
  126. // 设置音频源的播放时间
  127. audioSource.time = targetTime;
  128. }
  129. private string FormatTime(float time)
  130. {
  131. int minutes = Mathf.FloorToInt(time / 60);
  132. int seconds = Mathf.FloorToInt(time % 60);
  133. return string.Format("{0:00}:{1:00}", minutes, seconds);
  134. }
  135. }

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

闽ICP备14008679号