AudioManager.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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, AudioSourcePool> _currPlayAudio = new Map<string, AudioSourcePool>();
  16. private List<string> _bgmQueue = new List<string>();
  17. private AudioSourcePool _currBgm;
  18. private List<string> _audion = new List<string>();
  19. public AudioManager()
  20. {
  21. // UIAudio.AudioPlayManager = this;
  22. _root = new GameObject();
  23. _root.name = "audio";
  24. Object.DontDestroyOnLoad(_root);
  25. }
  26. /// <summary>
  27. /// 设置音效音量
  28. /// </summary>
  29. /// <param name="value"></param>
  30. public void SetAudioValue(float value)
  31. {
  32. }
  33. /// <summary>
  34. /// 设置背景音乐音量
  35. /// </summary>
  36. /// <param name="value"></param>
  37. public void SetBgmValue(float value)
  38. {
  39. }
  40. public async CTask Init()
  41. {
  42. }
  43. public async CTask InitMainAudio()
  44. {
  45. string config = "MainAudio.asset";
  46. AssetHandle assetHandle =
  47. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<AudionSettingConfig>(config);
  48. await LoadAudio(assetHandle.AssetObject<AudionSettingConfig>());
  49. }
  50. public async CTask LoadAudio(AudionSettingConfig audionSettingConfig)
  51. {
  52. CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();
  53. for (int i = 0; i < audionSettingConfig.configs.Count; i++)
  54. {
  55. CTask cTask = LoadAudio(audionSettingConfig.configs[i]);
  56. cTaskAwaitBuffer.AddTask(cTask);
  57. }
  58. await cTaskAwaitBuffer.WaitAll();
  59. }
  60. public async CTask LoadAudio(AudionSettingConfig.AudionConfig audionConfig)
  61. {
  62. await GetAduionBundleInfo(audionConfig.audionClipName, audionConfig.volume);
  63. }
  64. public async CTask<AudioBundleInfo> GetAduionBundleInfo(string audionName, float volume = 1)
  65. {
  66. // if (audionName.Contains('.'))
  67. // {
  68. // audionName = audionName.Split('.')[0];
  69. // }
  70. if (_allAduionBundleInfos.TryGetValue(audionName, out AudioBundleInfo abi))
  71. {
  72. return abi;
  73. }
  74. AudioBundleInfo audioBundleInfo = new AudioBundleInfo(audionName, volume);
  75. return audioBundleInfo;
  76. }
  77. public void Recycle(AudioSourcePool audioSourcePool)
  78. {
  79. if (!_audioSourcePools.Contains(audioSourcePool))
  80. {
  81. _audioSourcePools.Enqueue(audioSourcePool);
  82. }
  83. _currPlayAudio.Remove(audioSourcePool.CurrPlayName);
  84. }
  85. private AudioSourcePool GetAudioSourcePool()
  86. {
  87. if (_audioSourcePools.Count <= 0)
  88. {
  89. AudioSourcePool audioSourcePool = new AudioSourcePool();
  90. audioSourcePool.Init(_root.transform);
  91. return audioSourcePool;
  92. }
  93. AudioSourcePool asp = _audioSourcePools.Dequeue();
  94. return asp;
  95. }
  96. public void Stop(string audionName)
  97. {
  98. // for (int i = 0; i < _currPlayAudio.Count; i++)
  99. // {
  100. // if (_currPlayAudio[i].CurrPlayName.Equals(audionName))
  101. // {
  102. // _currPlayAudio[i].Finish();
  103. // i--;
  104. // }
  105. // }
  106. }
  107. public async void PlayBGM(string bgmName)
  108. {
  109. if (_currBgm != null && _currBgm.CurrPlayName != null && _currBgm.CurrPlayName.Equals(bgmName))
  110. {
  111. return;
  112. }
  113. if (_currBgm != null && _currBgm.IsPlay)
  114. {
  115. _bgmQueue.Add(_currBgm.CurrPlayName);
  116. }
  117. AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(bgmName);
  118. AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
  119. if (_currBgm == null)
  120. {
  121. _currBgm = GetAudioSourcePool();
  122. }
  123. _currBgm.Play(bgmName, audioClip, true, audioBundleInfo._volume);
  124. }
  125. public void PauseBGM()
  126. {
  127. if (_currBgm != null)
  128. {
  129. _currBgm.Pause();
  130. }
  131. }
  132. public void UnPauseBGM()
  133. {
  134. if (_currBgm != null)
  135. {
  136. _currBgm.UnPause();
  137. }
  138. }
  139. public async void StopBGM(string bgmName)
  140. {
  141. if (_currBgm == null || _currBgm._currPlayName == null)
  142. {
  143. return;
  144. }
  145. if (_currBgm.CurrPlayName.Equals(bgmName))
  146. {
  147. _currBgm.Stop();
  148. if (_bgmQueue.Count > 0)
  149. {
  150. string nextBgmName = _bgmQueue[^1];
  151. _bgmQueue.RemoveAt(_bgmQueue.Count - 1);
  152. PlayBGM(nextBgmName);
  153. }
  154. return;
  155. }
  156. if (_bgmQueue.Count > 0)
  157. {
  158. for (int i = _bgmQueue.Count - 1; i >= 0; i--)
  159. {
  160. if (_bgmQueue[i].Equals(bgmName))
  161. {
  162. _bgmQueue.RemoveAt(i);
  163. }
  164. }
  165. }
  166. }
  167. public async CTask<AudioSourcePool> PlayAudio(string audionName, bool isLoop = false, float speed = 1)
  168. {
  169. if (string.IsNullOrEmpty(audionName))
  170. {
  171. return null;
  172. }
  173. using (await CoroutineLockComponent.Instance.Wait(audionName))
  174. {
  175. if (_currPlayAudio.TryGetValue(audionName, out AudioSourcePool asp))
  176. {
  177. asp.Finish();
  178. }
  179. AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(audionName);
  180. AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
  181. if (audioClip == null)
  182. {
  183. return null;
  184. }
  185. AudioSourcePool audioSourcePool = GetAudioSourcePool();
  186. audioSourcePool.Play(audionName, audioClip, isLoop, speed);
  187. _currPlayAudio[audionName] = audioSourcePool;
  188. return audioSourcePool;
  189. }
  190. }
  191. public void Play(string audionName, bool isLoop)
  192. {
  193. PlayAudio(audionName, isLoop);
  194. }
  195. public void TimeLinePlayAudio(string audioName, bool isLoop, float speed)
  196. {
  197. PlayAudio(audioName, isLoop);
  198. }
  199. }
  200. }