12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.Networking;
- public class WavLoader : MonoBehaviour
- {
- public AudioSource audioSource; // 将AudioSource组件拖到这个变量上
- 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;
- }
- }
|