123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
-
- public class AudioPlayer : MonoBehaviour
- {
- public TextMeshProUGUI timeText;
- public Slider progressBar;
- public Button playPauseButton;
- public Sprite playSprite;
- public Sprite pauseSprite;
-
- private AudioClip recordedClip;
- private AudioSource audioSource;
- private float currentTime;
- private bool isPlaying;
- private bool isDraggingProgress;
-
- private void Awake()
- {
- audioSource = GetComponent<AudioSource>();
- }
- public void LoadAudioClip(byte[] audioData)
- {
- Debug.Log(audioData.ToString());
- recordedClip = WavUtility.ToAudioClip(audioData);
- Debug.Log(recordedClip.ToString());
- audioSource.clip = recordedClip;
- playPauseButton.onClick.AddListener(TogglePlayPause);
- progressBar.onValueChanged.AddListener(OnProgressBarValueChanged);
- UpdateProgressBar();
- UpdateTimeText();
- }
-
- public void TogglePlayPause()
- {
- // 切换播放/暂停
- if (isPlaying)
- {
- audioSource.Pause();
- playPauseButton.image.sprite = playSprite;
- isPlaying = false;
- }
- else
- {
- audioSource.Play();
- playPauseButton.image.sprite = pauseSprite;
- isPlaying = true;
- }
- }
-
- private void Update()
- {
- if (isPlaying)
- {
- UpdateProgressBar();
- UpdateTimeText();
-
- // 如果音频播放完毕,则停止播放并将播放标志设置为 false
- if (!audioSource.isPlaying)
- {
- playPauseButton.image.sprite = playSprite;
- isPlaying = false;
- audioSource.time = 0;
- progressBar.value = 0;
- UpdateTimeText();
- }
- }
- else
- {
- if (audioSource.clip != null)
- {
- UpdateProgressBar();
- UpdateTimeText();
- }
- }
- }
-
- private void UpdateProgressBar()
- {
- // 更新进度条的值
- if (recordedClip != null && !isDraggingProgress)
- {
- progressBar.value = audioSource.time / recordedClip.length;
- UpdateTimeText();
- }
- }
-
- private void UpdateTimeText()
- {
- // 更新时间文字显示
- if (recordedClip != null)
- {
- currentTime = audioSource.time;
- timeText.text = FormatTime(currentTime) + " / " + FormatTime(recordedClip.length);
- }
- }
-
- public void OnProgressBarValueChanged(float value)
- {
- // 当进度条的值发生变化时(用户拖动),更新播放器的播放位置
- float newTime = progressBar.value * recordedClip.length;
- if (newTime < 0)
- {
- newTime = 0;
- }
- else if (newTime > recordedClip.length)
- {
- newTime = recordedClip.length;
- }
- audioSource.time = newTime;
- }
-
- public void OnPointerDownProgressBar()
- {
- // 当用户按下进度条时,设置拖动标志为 true
- isDraggingProgress = true;
- }
-
- public void OnPointerUpProgressBar()
- {
- // 当用户放开进度条时,设置拖动标志为 false
- isDraggingProgress = false;
- }
-
- private string FormatTime(float time)
- {
- // 格式化时间为分钟:秒
- int minutes = Mathf.FloorToInt(time / 60f);
- int seconds = Mathf.FloorToInt(time % 60f);
- return string.Format("{0:00}:{1:00}", minutes, seconds);
- }
- }
|