AudioManager.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System.Collections.Generic;
  2. using Fort23.Core;
  3. using Fort23.UTool;
  4. using UnityEngine;
  5. using UnityEngine.Audio;
  6. using Utility;
  7. namespace Core.Audio
  8. {
  9. public class AudioManager : Singleton<AudioManager>
  10. {
  11. private GameObject _root;
  12. public AudioMixerGroup AudioMixerGroup;
  13. private Map<string, AudioBundleInfo> _allAduionBundleInfos = new Map<string, AudioBundleInfo>();
  14. private Queue<AudioSourcePool> _audioSourcePools = new Queue<AudioSourcePool>();
  15. private Map<string, List<AudioSourcePool>> _currPlayAudio = new Map<string, List<AudioSourcePool>>();
  16. private List<string> _bgmQueue = new List<string>();
  17. private AudioSourcePool _currBgm;
  18. private List<string> _audion = new List<string>();
  19. public float AudionVolume
  20. {
  21. get => _audionVolume;
  22. }
  23. public float BgmVolume
  24. {
  25. get => _bgmVolume;
  26. }
  27. private float _audionVolume = 1;
  28. private float _bgmVolume = 1;
  29. public AudioManager()
  30. {
  31. // UIAudio.AudioPlayManager = this;
  32. _root = new GameObject();
  33. _root.name = "audio";
  34. AudioListener audioListener = GameObject.FindObjectOfType<AudioListener>();
  35. if (audioListener == null)
  36. {
  37. _root.AddComponent<AudioListener>();
  38. }
  39. Object.DontDestroyOnLoad(_root);
  40. }
  41. /// <summary>
  42. /// 设置音效音量
  43. /// </summary>
  44. /// <param name="value"></param>
  45. public void SetAudioValue(float value)
  46. {
  47. _audionVolume = value;
  48. for (_currPlayAudio.Begin(); _currPlayAudio.Next();)
  49. {
  50. for (int i = 0; i < _currPlayAudio.Value.Count; i++)
  51. {
  52. _currPlayAudio.Value[i].Setvolume();
  53. }
  54. }
  55. }
  56. /// <summary>
  57. /// 设置背景音乐音量
  58. /// </summary>
  59. /// <param name="value"></param>
  60. public void SetBgmValue(float value)
  61. {
  62. _bgmVolume = value;
  63. for (_currPlayAudio.Begin(); _currPlayAudio.Next();)
  64. {
  65. for (int i = 0; i < _currPlayAudio.Value.Count; i++)
  66. {
  67. _currPlayAudio.Value[i].Setvolume();
  68. }
  69. }
  70. _currBgm?.Setvolume();
  71. }
  72. public async CTask Init()
  73. {
  74. }
  75. public async CTask InitMainAudio()
  76. {
  77. string config = "MainAudio.asset";
  78. AssetHandle assetHandle =
  79. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<AudionSettingConfig>(config);
  80. await LoadAudio(assetHandle.AssetObject<AudionSettingConfig>());
  81. }
  82. public async CTask LoadAudio(AudionSettingConfig audionSettingConfig)
  83. {
  84. CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();
  85. for (int i = 0; i < audionSettingConfig.configs.Count; i++)
  86. {
  87. CTask cTask = LoadAudio(audionSettingConfig.configs[i]);
  88. cTaskAwaitBuffer.AddTask(cTask);
  89. }
  90. await cTaskAwaitBuffer.WaitAll();
  91. }
  92. public void RemoveAudio(AudionSettingConfig audionSettingConfig)
  93. {
  94. for (int i = 0; i < audionSettingConfig.configs.Count; i++)
  95. {
  96. AudionSettingConfig.AudionConfig audionConfig = audionSettingConfig.configs[i];
  97. if (_allAduionBundleInfos.TryGetValue(audionConfig.audionClipName, out AudioBundleInfo abi))
  98. {
  99. if (_currPlayAudio.TryGetValue(audionConfig.audionClipName,
  100. out List<AudioSourcePool> audioSourcePool))
  101. {
  102. for (int j = 0; j < audioSourcePool.Count; j++)
  103. {
  104. audioSourcePool[i].Finish();
  105. }
  106. }
  107. abi.Dispose();
  108. _allAduionBundleInfos.Remove(audionConfig.audionClipName);
  109. }
  110. }
  111. }
  112. public async CTask LoadAudio(AudionSettingConfig.AudionConfig audionConfig)
  113. {
  114. await GetAduionBundleInfo(audionConfig.audionClipName, audionConfig.volume);
  115. }
  116. public async CTask<AudioBundleInfo> GetAduionBundleInfo(string audionName, float volume = 1)
  117. {
  118. // if (audionName.Contains('.'))
  119. // {
  120. // audionName = audionName.Split('.')[0];
  121. // }
  122. if (_allAduionBundleInfos.TryGetValue(audionName, out AudioBundleInfo abi))
  123. {
  124. return abi;
  125. }
  126. AudioBundleInfo audioBundleInfo = new AudioBundleInfo(audionName, volume);
  127. _allAduionBundleInfos.Add(audionName, audioBundleInfo);
  128. await audioBundleInfo.GetAudioClip();
  129. return audioBundleInfo;
  130. }
  131. public void Recycle(AudioSourcePool audioSourcePool)
  132. {
  133. if (!_audioSourcePools.Contains(audioSourcePool))
  134. {
  135. _audioSourcePools.Enqueue(audioSourcePool);
  136. }
  137. if (_currPlayAudio.TryGetValue(audioSourcePool.CurrPlayName, out List<AudioSourcePool> audioSourcePools))
  138. {
  139. audioSourcePools.Remove(audioSourcePool);
  140. }
  141. }
  142. private AudioSourcePool GetAudioSourcePool()
  143. {
  144. if (_audioSourcePools.Count <= 0)
  145. {
  146. AudioSourcePool audioSourcePool = new AudioSourcePool();
  147. audioSourcePool.Init(_root.transform);
  148. return audioSourcePool;
  149. }
  150. AudioSourcePool asp = _audioSourcePools.Dequeue();
  151. return asp;
  152. }
  153. public void Stop(string audionName)
  154. {
  155. // for (int i = 0; i < _currPlayAudio.Count; i++)
  156. // {
  157. // if (_currPlayAudio[i].CurrPlayName.Equals(audionName))
  158. // {
  159. // _currPlayAudio[i].Finish();
  160. // i--;
  161. // }
  162. // }
  163. }
  164. public async void PlayBGM(string bgmName)
  165. {
  166. if (_currBgm != null && _currBgm.CurrPlayName != null && _currBgm.CurrPlayName.Equals(bgmName))
  167. {
  168. return;
  169. }
  170. if (_currBgm != null && _currBgm.IsPlay)
  171. {
  172. _bgmQueue.Add(_currBgm.CurrPlayName);
  173. }
  174. AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(bgmName);
  175. AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
  176. if (_currBgm == null)
  177. {
  178. _currBgm = GetAudioSourcePool();
  179. }
  180. _currBgm.Play(bgmName, true, audioClip, true, audioBundleInfo._volume);
  181. }
  182. public void PauseBGM()
  183. {
  184. if (_currBgm != null)
  185. {
  186. _currBgm.Pause();
  187. }
  188. }
  189. public void UnPauseBGM()
  190. {
  191. if (_currBgm != null)
  192. {
  193. _currBgm.UnPause();
  194. }
  195. }
  196. public async void StopBGM(string bgmName)
  197. {
  198. if (_currBgm == null || _currBgm._currPlayName == null)
  199. {
  200. return;
  201. }
  202. if (_currBgm.CurrPlayName.Equals(bgmName))
  203. {
  204. _currBgm.Stop();
  205. if (_bgmQueue.Count > 0)
  206. {
  207. string nextBgmName = _bgmQueue[^1];
  208. _bgmQueue.RemoveAt(_bgmQueue.Count - 1);
  209. PlayBGM(nextBgmName);
  210. }
  211. return;
  212. }
  213. if (_bgmQueue.Count > 0)
  214. {
  215. for (int i = _bgmQueue.Count - 1; i >= 0; i--)
  216. {
  217. if (_bgmQueue[i].Equals(bgmName))
  218. {
  219. _bgmQueue.RemoveAt(i);
  220. }
  221. }
  222. }
  223. }
  224. public async CTask<AudioSourcePool> PlayAudio(string audionName, bool isLoop = false, float speed = 1)
  225. {
  226. if (string.IsNullOrEmpty(audionName))
  227. {
  228. return null;
  229. }
  230. using (await CoroutineLockComponent.Instance.Wait(audionName))
  231. {
  232. if (_currPlayAudio.TryGetValue(audionName, out List<AudioSourcePool> asp))
  233. {
  234. for (int i = 0; i < asp.Count; i++)
  235. {
  236. AudioSourcePool a = asp[i];
  237. a.Setvolume(a.AudioVolume - 0.33f);
  238. if (a.AudioVolume <= 0)
  239. {
  240. a.Finish();
  241. i--;
  242. }
  243. }
  244. // if (asp.Progress() < 0.2)
  245. // {
  246. // return null;
  247. // }
  248. //
  249. // asp.Finish();
  250. }
  251. else
  252. {
  253. asp = new List<AudioSourcePool>();
  254. _currPlayAudio.Add(audionName, asp);
  255. }
  256. AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(audionName);
  257. AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
  258. if (audioClip == null)
  259. {
  260. return null;
  261. }
  262. AudioSourcePool audioSourcePool = GetAudioSourcePool();
  263. audioSourcePool.Play(audionName, false, audioClip, isLoop, audioBundleInfo._volume, speed);
  264. asp.Add(audioSourcePool);
  265. return audioSourcePool;
  266. }
  267. }
  268. public void Play(string audionName, bool isLoop)
  269. {
  270. PlayAudio(audionName, isLoop);
  271. }
  272. public void TimeLinePlayAudio(string audioName, bool isLoop, float speed)
  273. {
  274. PlayAudio(audioName, isLoop);
  275. }
  276. }
  277. }