123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using System.Collections.Generic;
- using Fort23.Core;
- using Fort23.UTool;
- using UnityEngine;
- using UnityEngine.Audio;
- using Utility;
- 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 List<AudioSourcePool> _currPlayAudio = new List<AudioSourcePool>();
- private List<string> _bgmQueue = new List<string>();
- private AudioSourcePool _currBgm;
- private List<string> _audion = new List<string>();
- public AudioManager()
- {
- // UIAudio.AudioPlayManager = this;
- _root = new GameObject();
- _root.name = "audio";
- Object.DontDestroyOnLoad(_root);
- }
- /// <summary>
- /// 设置音效音量
- /// </summary>
- /// <param name="value"></param>
- public void SetAudioValue(float value)
- {
- }
- /// <summary>
- /// 设置背景音乐音量
- /// </summary>
- /// <param name="value"></param>
- public void SetBgmValue(float value)
- {
- }
- public async CTask Init()
- {
- }
- 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 async CTask LoadAudio(AudionSettingConfig.AudionConfig audionConfig)
- {
- await GetAduionBundleInfo(audionConfig.audionClipName, audionConfig.volume);
- }
- public async CTask<AudioBundleInfo> GetAduionBundleInfo(string audionName, float volume = 1)
- {
- if (_allAduionBundleInfos.TryGetValue(audionName, out AudioBundleInfo abi))
- {
- return abi;
- }
- AudioBundleInfo audioBundleInfo = new AudioBundleInfo(audionName, volume);
- return audioBundleInfo;
- }
- public void Recycle(AudioSourcePool audioSourcePool)
- {
- if (!_audioSourcePools.Contains(audioSourcePool))
- {
- _audioSourcePools.Enqueue(audioSourcePool);
- }
- _currPlayAudio.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)
- {
- for (int i = 0; i < _currPlayAudio.Count; i++)
- {
- if (_currPlayAudio[i].CurrPlayName.Equals(audionName))
- {
- _currPlayAudio[i].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, audioClip, true);
- }
- 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;
- }
- AudioBundleInfo audioBundleInfo = await GetAduionBundleInfo(audionName);
- AssetHandle audioClip = await audioBundleInfo.GetAudioClip();
- if (audioClip == null)
- {
- return null;
- }
- AudioSourcePool audioSourcePool = GetAudioSourcePool();
- audioSourcePool.Play(audionName, audioClip, isLoop, speed);
- return audioSourcePool;
- }
- public void Play(string audionName, bool isLoop)
- {
- PlayAudio(audionName, isLoop);
- }
- public void TimeLinePlayAudio(string audioName, bool isLoop, float speed)
- {
- PlayAudio(audioName, isLoop);
- }
- }
- }
|