1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.Networking;
- public class WavLoader : MonoBehaviour
- {
- public static WavLoader Instance;
- public AudioSource audioSource; // 将AudioSource组件拖到这个变量上
- private void Awake()
- {
- Instance = this;
- }
- void Start()
- {
- StartCoroutine(LoadAndPlayWav());
- }
- bool iscmute;
- public void ismute(bool t)
- {
- iscmute = t;
- audioSource.mute = iscmute;
- }
- IEnumerator LoadAndPlayWav()
- {
- Debug.Log(Application.streamingAssetsPath + "/ui/bg.wav");
- // 使用WWW加载WAV文件
- using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(Application.streamingAssetsPath+"/ui/bg.wav",AudioType.WAV))
- {
- yield return www.SendWebRequest();
- // 检查是否有错误
- if (string.IsNullOrEmpty(www.error))
- {
- // 获取音频剪辑
- AudioClip clip =DownloadHandlerAudioClip.GetContent(www);
-
- // 播放音频
- audioSource.clip = clip;
- audioSource.Play();
- }
- else
- {
- Debug.LogError("Error loading WAV: " + www.error);
- }
- }
- }
- private void OnApplicationPause(bool pause)
- {
- if(pause)
- {
- audioSource.Pause();
- }else
- {
- audioSource.UnPause();
- }
- }
- private void Update()
- {
- audioSource.mute = iscmute;
- }
- }
|