| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 | 
							- using System;
 
- using System.Collections.Generic;
 
- using Fort23.Core;
 
- using Fort23.UTool;
 
- using UnityEngine;
 
- using UnityEngine.Audio;
 
- using Utility;
 
- using Object = UnityEngine.Object;
 
- namespace Core.Audio
 
- {
 
-     public class AudioManager : Singleton<AudioManager>
 
-     {
 
-         private GameObject _root;
 
-         public AudioMixerGroup AudioMixerGroup;
 
-         private Map<string, AudioBundleInfo> _allAduionBundleInfos = new Map<string, AudioBundleInfo>();
 
-         private Queue<AudioSourcePool> _audioSourcePools = new Queue<AudioSourcePool>();
 
-         private Map<string, List<AudioSourcePool>> _currPlayAudio = new Map<string, List<AudioSourcePool>>();
 
-         private List<string> _bgmQueue = new List<string>();
 
-         private AudioSourcePool _currBgm;
 
-         private List<string> _audion = new List<string>();
 
-         public float AudionVolume
 
-         {
 
-             get => _audionVolume;
 
-         }
 
-         public float BgmVolume
 
-         {
 
-             get => _bgmVolume;
 
-         }
 
-         private float _audionVolume = 1;
 
-         private float _bgmVolume = 1;
 
-         public AudioManager()
 
-         {
 
-             // UIAudio.AudioPlayManager = this;
 
-             _root = new GameObject();
 
-             _root.name = "audio";
 
-             AudioListener audioListener = GameObject.FindObjectOfType<AudioListener>();
 
-             if (audioListener == null)
 
-             {
 
-                 _root.AddComponent<AudioListener>();
 
-             }
 
-             Object.DontDestroyOnLoad(_root);
 
-         }
 
-         /// <summary>
 
-         /// 设置音效音量
 
-         /// </summary>
 
-         /// <param name="value"></param>
 
-         public void SetAudioValue(float value)
 
-         {
 
-             _audionVolume = value;
 
-             for (_currPlayAudio.Begin(); _currPlayAudio.Next();)
 
-             {
 
-                 for (int i = 0; i < _currPlayAudio.Value.Count; i++)
 
-                 {
 
-                     _currPlayAudio.Value[i].Setvolume();
 
-                 }
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// 设置背景音乐音量
 
-         /// </summary>
 
-         /// <param name="value"></param>
 
-         public void SetBgmValue(float value)
 
-         {
 
-             _bgmVolume = value;
 
-             // for (_currPlayAudio.Begin(); _currPlayAudio.Next();)
 
-             // {
 
-             //     for (int i = 0; i < _currPlayAudio.Value.Count; i++)
 
-             //     {
 
-             //         _currPlayAudio.Value[i].Setvolume();
 
-             //     }
 
-             // }
 
-             _currBgm?.Setvolume();
 
-         }
 
-         public async CTask Init()
 
-         {
 
-         }
 
-         public async CTask InitMainAudio()
 
-         {
 
-             string config = "MainAudio.asset";
 
-             AssetHandle assetHandle =
 
-                 await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<AudionSettingConfig>(config);
 
-             await LoadAudio(assetHandle.AssetObject<AudionSettingConfig>());
 
-         }
 
-         public async CTask LoadAudio(AudionSettingConfig audionSettingConfig)
 
-         {
 
-             CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();
 
-             for (int i = 0; i < audionSettingConfig.configs.Count; i++)
 
-             {
 
-                 CTask cTask = LoadAudio(audionSettingConfig.configs[i]);
 
-                 cTaskAwaitBuffer.AddTask(cTask);
 
-             }
 
-             await cTaskAwaitBuffer.WaitAll();
 
-         }
 
-         public void RemoveAudio(AudionSettingConfig audionSettingConfig)
 
-         {
 
-             for (int i = 0; i < audionSettingConfig.configs.Count; i++)
 
-             {
 
-                 AudionSettingConfig.AudionConfig audionConfig = audionSettingConfig.configs[i];
 
-                 if (_allAduionBundleInfos.TryGetValue(audionConfig.audionClipName, out AudioBundleInfo abi))
 
-                 {
 
-                     if (_currPlayAudio.TryGetValue(audionConfig.audionClipName,
 
-                             out List<AudioSourcePool> audioSourcePool))
 
-                     {
 
-                         for (int j = 0; j < audioSourcePool.Count; j++)
 
-                         {
 
-                             audioSourcePool[i].Finish();
 
-                         }
 
-                     }
 
-                     abi.Dispose();
 
-                     _allAduionBundleInfos.Remove(audionConfig.audionClipName);
 
-                 }
 
-             }
 
-         }
 
-         public async CTask LoadAudio(AudionSettingConfig.AudionConfig audionConfig)
 
-         {
 
-             await GetAduionBundleInfo(audionConfig.audionClipName, audionConfig.volume);
 
-         }
 
-         public async CTask<AudioBundleInfo> GetAduionBundleInfo(string audionName, float volume = 1)
 
-         {
 
-             // if (audionName.Contains('.'))
 
-             // {
 
-             //     audionName = audionName.Split('.')[0];
 
-             // }
 
-             if (_allAduionBundleInfos.TryGetValue(audionName, out AudioBundleInfo abi))
 
-             {
 
-                 return abi;
 
-             }
 
-             AudioBundleInfo audioBundleInfo = new AudioBundleInfo(audionName, volume);
 
-             _allAduionBundleInfos.Add(audionName, audioBundleInfo);
 
-             await audioBundleInfo.GetAudioClip();
 
-             return audioBundleInfo;
 
-         }
 
-         public void Recycle(AudioSourcePool audioSourcePool)
 
-         {
 
-             if (!_audioSourcePools.Contains(audioSourcePool))
 
-             {
 
-                 _audioSourcePools.Enqueue(audioSourcePool);
 
-             }
 
-             if (_currPlayAudio.TryGetValue(audioSourcePool.CurrPlayName, out List<AudioSourcePool> audioSourcePools))
 
-             {
 
-                 audioSourcePools.Remove(audioSourcePool);
 
-             }
 
-         }
 
-         private AudioSourcePool GetAudioSourcePool()
 
-         {
 
-             if (_audioSourcePools.Count <= 0)
 
-             {
 
-                 AudioSourcePool audioSourcePool = new AudioSourcePool();
 
-                 audioSourcePool.Init(_root.transform);
 
-                 return audioSourcePool;
 
-             }
 
-             AudioSourcePool asp = _audioSourcePools.Dequeue();
 
-             return asp;
 
-         }
 
-         public void Stop(string audionName)
 
-         {
 
-             if (_currPlayAudio.TryGetValue(audionName, out List<AudioSourcePool> asp))
 
-             {
 
-                 for (int i = 0; i < asp.Count; i++)
 
-                 {
 
-                     AudioSourcePool a = asp[i];
 
-                     a.Finish();
 
-                     i--;
 
-                 }
 
-             }
 
-         }
 
-         public async void PlayBGM(string bgmName)
 
-         {
 
-             if (_currBgm != null && _currBgm.CurrPlayName != null && _currBgm.CurrPlayName.Equals(bgmName))
 
-             {
 
-                 return;
 
-             }
 
-             if (_currBgm != null && _currBgm.IsPlay)
 
-             {
 
-                 _bgmQueue.Add(_currBgm.CurrPlayName);
 
-             }
 
-             AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(bgmName);
 
-             AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
 
-             if (_currBgm == null)
 
-             {
 
-                 _currBgm = GetAudioSourcePool();
 
-             }
 
-             _currBgm.Play(bgmName, true, audioClip, true, audioBundleInfo._volume);
 
-         }
 
-         public void PauseBGM()
 
-         {
 
-             if (_currBgm != null)
 
-             {
 
-                 _currBgm.Pause();
 
-             }
 
-         }
 
-         public void UnPauseBGM()
 
-         {
 
-             if (_currBgm != null)
 
-             {
 
-                 _currBgm.UnPause();
 
-             }
 
-         }
 
-         public async void StopBGM(string bgmName)
 
-         {
 
-             if (_currBgm == null || _currBgm._currPlayName == null)
 
-             {
 
-                 return;
 
-             }
 
-             if (_currBgm.CurrPlayName.Equals(bgmName))
 
-             {
 
-                 _currBgm.Stop();
 
-                 if (_bgmQueue.Count > 0)
 
-                 {
 
-                     string nextBgmName = _bgmQueue[^1];
 
-                     _bgmQueue.RemoveAt(_bgmQueue.Count - 1);
 
-                     PlayBGM(nextBgmName);
 
-                 }
 
-                 return;
 
-             }
 
-             if (_bgmQueue.Count > 0)
 
-             {
 
-                 for (int i = _bgmQueue.Count - 1; i >= 0; i--)
 
-                 {
 
-                     if (_bgmQueue[i].Equals(bgmName))
 
-                     {
 
-                         _bgmQueue.RemoveAt(i);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         public async CTask<AudioSourcePool> PlayAudio(string audionName, bool isLoop = false, float speed = 1)
 
-         {
 
-             if (string.IsNullOrEmpty(audionName))
 
-             {
 
-                 return null;
 
-             }
 
-             if (audionName.IndexOf(".", StringComparison.Ordinal) < 0)
 
-             {
 
-                 audionName += ".wav";
 
-             }
 
-               
 
-             using (await CoroutineLockComponent.Instance.Wait(audionName))
 
-             {
 
-                 if (_currPlayAudio.TryGetValue(audionName, out List<AudioSourcePool> asp))
 
-                 {
 
-                     for (int i = 0; i < asp.Count; i++)
 
-                     {
 
-                         AudioSourcePool a = asp[i];
 
-                         float t = a.GetPlayTime();
 
-                         if (t > 0 && t < 0.05f)
 
-                         {
 
-                             return null;
 
-                         }
 
-                         a.Setvolume(a.AudioVolume - 0.33f);
 
-                         if (a.AudioVolume <= 0)
 
-                         {
 
-                             a.Finish();
 
-                             i--;
 
-                         }
 
-                     }
 
-                     // if (asp.Progress() < 0.2)
 
-                     // {
 
-                     //     return null;
 
-                     // }
 
-                     //
 
-                     // asp.Finish();
 
-                 }
 
-                 else
 
-                 {
 
-                     asp = new List<AudioSourcePool>();
 
-                     _currPlayAudio.Add(audionName, asp);
 
-                 }
 
-                 AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(audionName);
 
-                 AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
 
-                 if (audioClip == null)
 
-                 {
 
-                     return null;
 
-                 }
 
-                 AudioSourcePool audioSourcePool = GetAudioSourcePool();
 
-                 audioSourcePool.Play(audionName, false, audioClip, isLoop, audioBundleInfo._volume, speed);
 
-                 asp.Add(audioSourcePool);
 
-                 return audioSourcePool;
 
-             }
 
-         }
 
-         public void Play(string audionName, bool isLoop)
 
-         {
 
-             PlayAudio(audionName, isLoop);
 
-         }
 
-         public void TimeLinePlayAudio(string audioName, bool isLoop, float speed)
 
-         {
 
-             PlayAudio(audioName, isLoop);
 
-         }
 
-     }
 
- }
 
 
  |