Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioPlayer.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using TMPro;
  5. public class AudioPlayer : MonoBehaviour
  6. {
  7. public TextMeshProUGUI timeText;
  8. public Slider progressBar;
  9. public Button playPauseButton;
  10. public Sprite playSprite;
  11. public Sprite pauseSprite;
  12. private AudioClip recordedClip;
  13. private AudioSource audioSource;
  14. private float currentTime;
  15. private bool isPlaying;
  16. private bool isDraggingProgress;
  17. private void Awake()
  18. {
  19. audioSource = GetComponent<AudioSource>();
  20. }
  21. public void LoadAudioClip(byte[] audioData)
  22. {
  23. Debug.Log(audioData.ToString());
  24. recordedClip = WavUtility.ToAudioClip(audioData);
  25. Debug.Log(recordedClip.ToString());
  26. audioSource.clip = recordedClip;
  27. playPauseButton.onClick.AddListener(TogglePlayPause);
  28. progressBar.onValueChanged.AddListener(OnProgressBarValueChanged);
  29. UpdateProgressBar();
  30. UpdateTimeText();
  31. }
  32. public void TogglePlayPause()
  33. {
  34. // 切换播放/暂停
  35. if (isPlaying)
  36. {
  37. audioSource.Pause();
  38. playPauseButton.image.sprite = playSprite;
  39. isPlaying = false;
  40. }
  41. else
  42. {
  43. audioSource.Play();
  44. playPauseButton.image.sprite = pauseSprite;
  45. isPlaying = true;
  46. }
  47. }
  48. private void Update()
  49. {
  50. if (isPlaying)
  51. {
  52. UpdateProgressBar();
  53. UpdateTimeText();
  54. // 如果音频播放完毕,则停止播放并将播放标志设置为 false
  55. if (!audioSource.isPlaying)
  56. {
  57. playPauseButton.image.sprite = playSprite;
  58. isPlaying = false;
  59. audioSource.time = 0;
  60. progressBar.value = 0;
  61. UpdateTimeText();
  62. }
  63. }
  64. else
  65. {
  66. if (audioSource.clip != null)
  67. {
  68. UpdateProgressBar();
  69. UpdateTimeText();
  70. }
  71. }
  72. }
  73. private void UpdateProgressBar()
  74. {
  75. // 更新进度条的值
  76. if (recordedClip != null && !isDraggingProgress)
  77. {
  78. progressBar.value = audioSource.time / recordedClip.length;
  79. UpdateTimeText();
  80. }
  81. }
  82. private void UpdateTimeText()
  83. {
  84. // 更新时间文字显示
  85. if (recordedClip != null)
  86. {
  87. currentTime = audioSource.time;
  88. timeText.text = FormatTime(currentTime) + " / " + FormatTime(recordedClip.length);
  89. }
  90. }
  91. public void OnProgressBarValueChanged(float value)
  92. {
  93. // 当进度条的值发生变化时(用户拖动),更新播放器的播放位置
  94. float newTime = progressBar.value * recordedClip.length;
  95. if (newTime < 0)
  96. {
  97. newTime = 0;
  98. }
  99. else if (newTime > recordedClip.length)
  100. {
  101. newTime = recordedClip.length;
  102. }
  103. audioSource.time = newTime;
  104. }
  105. public void OnPointerDownProgressBar()
  106. {
  107. // 当用户按下进度条时,设置拖动标志为 true
  108. isDraggingProgress = true;
  109. }
  110. public void OnPointerUpProgressBar()
  111. {
  112. // 当用户放开进度条时,设置拖动标志为 false
  113. isDraggingProgress = false;
  114. }
  115. private string FormatTime(float time)
  116. {
  117. // 格式化时间为分钟:秒
  118. int minutes = Mathf.FloorToInt(time / 60f);
  119. int seconds = Mathf.FloorToInt(time % 60f);
  120. return string.Format("{0:00}:{1:00}", minutes, seconds);
  121. }
  122. }