WavLoader.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Networking;
  4. public class WavLoader : MonoBehaviour
  5. {
  6. public AudioSource audioSource; // 将AudioSource组件拖到这个变量上
  7. void Start()
  8. {
  9. StartCoroutine(LoadAndPlayWav());
  10. }
  11. bool iscmute;
  12. public void ismute(bool t)
  13. {
  14. iscmute = t;
  15. audioSource.mute = iscmute;
  16. }
  17. IEnumerator LoadAndPlayWav()
  18. {
  19. Debug.Log(Application.streamingAssetsPath + "/ui/bg.wav");
  20. // 使用WWW加载WAV文件
  21. using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(Application.streamingAssetsPath+"/ui/bg.wav",AudioType.WAV))
  22. {
  23. yield return www.SendWebRequest();
  24. // 检查是否有错误
  25. if (string.IsNullOrEmpty(www.error))
  26. {
  27. // 获取音频剪辑
  28. AudioClip clip =DownloadHandlerAudioClip.GetContent(www);
  29. // 播放音频
  30. audioSource.clip = clip;
  31. audioSource.Play();
  32. }
  33. else
  34. {
  35. Debug.LogError("Error loading WAV: " + www.error);
  36. }
  37. }
  38. }
  39. private void OnApplicationPause(bool pause)
  40. {
  41. if(pause)
  42. {
  43. audioSource.Pause();
  44. }else
  45. {
  46. audioSource.UnPause();
  47. }
  48. }
  49. private void Update()
  50. {
  51. audioSource.mute = iscmute;
  52. }
  53. }