AudioManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. if (_currPlayAudio.TryGetValue(audionName, out List<AudioSourcePool> asp))
  156. {
  157. for (int i = 0; i < asp.Count; i++)
  158. {
  159. AudioSourcePool a = asp[i];
  160. a.Finish();
  161. i--;
  162. }
  163. }
  164. }
  165. public async void PlayBGM(string bgmName)
  166. {
  167. if (_currBgm != null && _currBgm.CurrPlayName != null && _currBgm.CurrPlayName.Equals(bgmName))
  168. {
  169. return;
  170. }
  171. if (_currBgm != null && _currBgm.IsPlay)
  172. {
  173. _bgmQueue.Add(_currBgm.CurrPlayName);
  174. }
  175. AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(bgmName);
  176. AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
  177. if (_currBgm == null)
  178. {
  179. _currBgm = GetAudioSourcePool();
  180. }
  181. _currBgm.Play(bgmName, true, audioClip, true, audioBundleInfo._volume);
  182. }
  183. public void PauseBGM()
  184. {
  185. if (_currBgm != null)
  186. {
  187. _currBgm.Pause();
  188. }
  189. }
  190. public void UnPauseBGM()
  191. {
  192. if (_currBgm != null)
  193. {
  194. _currBgm.UnPause();
  195. }
  196. }
  197. public async void StopBGM(string bgmName)
  198. {
  199. if (_currBgm == null || _currBgm._currPlayName == null)
  200. {
  201. return;
  202. }
  203. if (_currBgm.CurrPlayName.Equals(bgmName))
  204. {
  205. _currBgm.Stop();
  206. if (_bgmQueue.Count > 0)
  207. {
  208. string nextBgmName = _bgmQueue[^1];
  209. _bgmQueue.RemoveAt(_bgmQueue.Count - 1);
  210. PlayBGM(nextBgmName);
  211. }
  212. return;
  213. }
  214. if (_bgmQueue.Count > 0)
  215. {
  216. for (int i = _bgmQueue.Count - 1; i >= 0; i--)
  217. {
  218. if (_bgmQueue[i].Equals(bgmName))
  219. {
  220. _bgmQueue.RemoveAt(i);
  221. }
  222. }
  223. }
  224. }
  225. public async CTask<AudioSourcePool> PlayAudio(string audionName, bool isLoop = false, float speed = 1)
  226. {
  227. if (string.IsNullOrEmpty(audionName))
  228. {
  229. return null;
  230. }
  231. using (await CoroutineLockComponent.Instance.Wait(audionName))
  232. {
  233. if (_currPlayAudio.TryGetValue(audionName, out List<AudioSourcePool> asp))
  234. {
  235. for (int i = 0; i < asp.Count; i++)
  236. {
  237. AudioSourcePool a = asp[i];
  238. float t = a.GetPlayTime();
  239. if (t > 0 && t < 0.05f)
  240. {
  241. return null;
  242. }
  243. a.Setvolume(a.AudioVolume - 0.33f);
  244. if (a.AudioVolume <= 0)
  245. {
  246. a.Finish();
  247. i--;
  248. }
  249. }
  250. // if (asp.Progress() < 0.2)
  251. // {
  252. // return null;
  253. // }
  254. //
  255. // asp.Finish();
  256. }
  257. else
  258. {
  259. asp = new List<AudioSourcePool>();
  260. _currPlayAudio.Add(audionName, asp);
  261. }
  262. AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(audionName);
  263. AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
  264. if (audioClip == null)
  265. {
  266. return null;
  267. }
  268. AudioSourcePool audioSourcePool = GetAudioSourcePool();
  269. audioSourcePool.Play(audionName, false, audioClip, isLoop, audioBundleInfo._volume, speed);
  270. asp.Add(audioSourcePool);
  271. return audioSourcePool;
  272. }
  273. }
  274. public void Play(string audionName, bool isLoop)
  275. {
  276. PlayAudio(audionName, isLoop);
  277. }
  278. public void TimeLinePlayAudio(string audioName, bool isLoop, float speed)
  279. {
  280. PlayAudio(audioName, isLoop);
  281. }
  282. }
  283. }