AudioManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 List<AudioSourcePool> _currPlayAudio = new List<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 LoadAudio(AudionSettingConfig audionSettingConfig)
  44. {
  45. CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();
  46. for (int i = 0; i < audionSettingConfig.configs.Count; i++)
  47. {
  48. CTask cTask = LoadAudio(audionSettingConfig.configs[i]);
  49. cTaskAwaitBuffer.AddTask(cTask);
  50. }
  51. await cTaskAwaitBuffer.WaitAll();
  52. }
  53. public async CTask LoadAudio(AudionSettingConfig.AudionConfig audionConfig)
  54. {
  55. await GetAduionBundleInfo(audionConfig.audionClipName, audionConfig.volume);
  56. }
  57. public async CTask<AudioBundleInfo> GetAduionBundleInfo(string audionName, float volume = 1)
  58. {
  59. if (_allAduionBundleInfos.TryGetValue(audionName, out AudioBundleInfo abi))
  60. {
  61. return abi;
  62. }
  63. AudioBundleInfo audioBundleInfo = new AudioBundleInfo(audionName, volume);
  64. return audioBundleInfo;
  65. }
  66. public void Recycle(AudioSourcePool audioSourcePool)
  67. {
  68. if (!_audioSourcePools.Contains(audioSourcePool))
  69. {
  70. _audioSourcePools.Enqueue(audioSourcePool);
  71. }
  72. _currPlayAudio.Remove(audioSourcePool);
  73. }
  74. private AudioSourcePool GetAudioSourcePool()
  75. {
  76. if (_audioSourcePools.Count <= 0)
  77. {
  78. AudioSourcePool audioSourcePool = new AudioSourcePool();
  79. audioSourcePool.Init(_root.transform);
  80. return audioSourcePool;
  81. }
  82. AudioSourcePool asp = _audioSourcePools.Dequeue();
  83. return asp;
  84. }
  85. public void Stop(string audionName)
  86. {
  87. for (int i = 0; i < _currPlayAudio.Count; i++)
  88. {
  89. if (_currPlayAudio[i].CurrPlayName.Equals(audionName))
  90. {
  91. _currPlayAudio[i].Finish();
  92. i--;
  93. }
  94. }
  95. }
  96. public async void PlayBGM(string bgmName)
  97. {
  98. if (_currBgm != null && _currBgm.CurrPlayName != null && _currBgm.CurrPlayName.Equals(bgmName))
  99. {
  100. return;
  101. }
  102. if (_currBgm != null && _currBgm.IsPlay)
  103. {
  104. _bgmQueue.Add(_currBgm.CurrPlayName);
  105. }
  106. AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(bgmName);
  107. AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
  108. if (_currBgm == null)
  109. {
  110. _currBgm = GetAudioSourcePool();
  111. }
  112. _currBgm.Play(bgmName, audioClip, true);
  113. }
  114. public void PauseBGM()
  115. {
  116. if (_currBgm != null)
  117. {
  118. _currBgm.Pause();
  119. }
  120. }
  121. public void UnPauseBGM()
  122. {
  123. if (_currBgm != null)
  124. {
  125. _currBgm.UnPause();
  126. }
  127. }
  128. public async void StopBGM(string bgmName)
  129. {
  130. if (_currBgm == null || _currBgm._currPlayName == null)
  131. {
  132. return;
  133. }
  134. if (_currBgm.CurrPlayName.Equals(bgmName))
  135. {
  136. _currBgm.Stop();
  137. if (_bgmQueue.Count > 0)
  138. {
  139. string nextBgmName = _bgmQueue[^1];
  140. _bgmQueue.RemoveAt(_bgmQueue.Count - 1);
  141. PlayBGM(nextBgmName);
  142. }
  143. return;
  144. }
  145. if (_bgmQueue.Count > 0)
  146. {
  147. for (int i = _bgmQueue.Count - 1; i >= 0; i--)
  148. {
  149. if (_bgmQueue[i].Equals(bgmName))
  150. {
  151. _bgmQueue.RemoveAt(i);
  152. }
  153. }
  154. }
  155. }
  156. public async CTask<AudioSourcePool> PlayAudio(string audionName, bool isLoop = false, float speed = 1)
  157. {
  158. if (string.IsNullOrEmpty(audionName))
  159. {
  160. return null;
  161. }
  162. AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(audionName);
  163. AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
  164. if (audioClip == null)
  165. {
  166. return null;
  167. }
  168. AudioSourcePool audioSourcePool = GetAudioSourcePool();
  169. audioSourcePool.Play(audionName, audioClip, isLoop, speed);
  170. return audioSourcePool;
  171. }
  172. public void Play(string audionName, bool isLoop)
  173. {
  174. PlayAudio(audionName, isLoop);
  175. }
  176. public void TimeLinePlayAudio(string audioName, bool isLoop, float speed)
  177. {
  178. PlayAudio(audioName, isLoop);
  179. }
  180. }
  181. }