WavLoader.cs 1.5 KB

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