NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Audio/AudioManager.cs

297 lines
8.1 KiB
C#
Raw Normal View History

2024-01-19 17:25:06 +08:00
using System;
2024-03-26 12:22:47 +08:00
using CriWare;
using CriWare.Assets;
2024-01-19 17:25:06 +08:00
using Cysharp.Threading.Tasks;
using PhxhSDK;
2023-08-22 19:08:45 +08:00
using UnityEngine;
public class AudioManager: Singlenton<AudioManager>, IStart, IInitable, IUpdatable, IAudioPlay
2023-08-22 19:08:45 +08:00
{
private AudioCriManager _audioMgrObj = new();
private AudioCriwareManager _audioMgrOldObj = new();
2023-08-22 19:08:45 +08:00
public GameObject initGameObject;
2024-01-19 17:25:06 +08:00
2024-04-28 17:03:37 +08:00
public static bool DisableAudio = false;
2024-04-28 16:18:27 +08:00
2024-07-01 17:23:05 +08:00
private bool isPlayingAlbum = false;
public bool IsPlayingAlbum
{
get
{
return isPlayingAlbum;
}
set
{
isPlayingAlbum = value;
}
}
public bool IsPlayingBGM { get; set; }
2024-07-01 17:23:05 +08:00
public AudioCriManager AudioMgrObj
2024-03-26 12:22:47 +08:00
{
get { return _audioMgrObj; }
}
public AudioCriwareManager AudioOldMgrObj
{
get { return _audioMgrOldObj; }
}
2024-03-26 12:22:47 +08:00
2024-01-19 17:25:06 +08:00
private void AfterSwitchCamera(bool isRegister = true)
{
if (isRegister)
{
CameraManager.Instance.AfterSwitchMainCamera += _audioMgrObj.AfterSwitchMainCamera;
}
else
{
CameraManager.Instance.AfterSwitchMainCamera -= _audioMgrObj.AfterSwitchMainCamera;
}
}
2023-08-22 19:08:45 +08:00
public void Init()
{
_audioMgrObj.Init();
2024-01-19 17:25:06 +08:00
AfterSwitchCamera();
2023-08-22 19:08:45 +08:00
}
2023-08-22 19:08:45 +08:00
public void Release()
{
_audioMgrObj.Release();
2024-01-19 17:25:06 +08:00
AfterSwitchCamera(false);
2023-10-19 15:00:19 +08:00
}
public void Start()
2023-08-22 19:08:45 +08:00
{
_audioMgrObj.Start();
initGameObject = initGameObject;
2023-08-22 19:08:45 +08:00
}
public void Update(float dt)
2023-08-22 19:08:45 +08:00
{
_audioMgrObj.Update(dt);
2023-08-22 19:08:45 +08:00
}
public float GlobalVolume
2023-08-22 19:08:45 +08:00
{
get { return _audioMgrObj.GlobalVolume; }
set { _audioMgrObj.GlobalVolume = value; }
2023-08-22 19:08:45 +08:00
}
public float GlobalMusicVolume
2023-08-22 19:08:45 +08:00
{
get { return _audioMgrObj.GlobalMusicVolume; }
set { _audioMgrObj.GlobalMusicVolume = value; }
2023-08-22 19:08:45 +08:00
}
public float GlobalSoundsVolume
2023-08-22 19:08:45 +08:00
{
get { return _audioMgrObj.GlobalSoundsVolume; }
set { _audioMgrObj.GlobalSoundsVolume = value; }
2023-08-22 19:08:45 +08:00
}
2023-12-13 15:14:52 +08:00
public bool GlobalIsSpeakOpen
{
get { return _audioMgrObj.GlobalIsOpenSpeak; }
set { _audioMgrObj.GlobalIsOpenSpeak = value; }
}
2024-04-28 14:24:10 +08:00
public float GlobalSpeakVolume
{
get { return _audioMgrObj.GlobalSpeakVolume; }
set { _audioMgrObj.GlobalSpeakVolume = value; }
}
2023-10-19 15:00:19 +08:00
2023-08-22 19:08:45 +08:00
public void SetMusicEnable(bool isEnable)
{
GlobalMusicVolume = isEnable ? 1 : 0;
}
public void SetSoundEnable(bool isEnable)
{
GlobalSoundsVolume = isEnable ? 1 : 0;
}
public CriAtomExAcb GetAcb(string cueSheetName)
2023-08-22 19:08:45 +08:00
{
return _audioMgrObj.GetAcb(cueSheetName);
2023-10-19 15:00:19 +08:00
}
#region 流式播放
public int PlayAudio(string key, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound,
2024-10-10 18:55:19 +08:00
GameObject posObj = null, bool loop = false, bool isFade = false)
2023-10-19 15:00:19 +08:00
{
2024-10-17 18:38:07 +08:00
if (type == AudioCriManager.EPlayType.Music)
{
//如果使用单个audio播放music可能会顶掉原先播放的专辑音乐
//在这里将专辑音乐标记为false
IsPlayingAlbum = false;
IsPlayingBGM = false;
2024-10-17 18:38:07 +08:00
}
2024-10-10 18:55:19 +08:00
return _audioMgrObj.PlayAudio(key, type, posObj, loop, isFade);
2023-10-19 15:00:19 +08:00
}
2024-07-25 17:43:14 +08:00
2024-10-10 18:55:19 +08:00
public int PlayAudio(string key, Vector3 pos, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound, bool loop = false,
bool isFade = false)
2024-07-25 17:43:14 +08:00
{
2024-10-17 18:38:07 +08:00
if (type == AudioCriManager.EPlayType.Music)
{
//如果使用单个audio播放music可能会顶掉原先播放的专辑音乐
//在这里将专辑音乐标记为false
IsPlayingAlbum = false;
IsPlayingBGM = false;
2024-10-17 18:38:07 +08:00
}
2024-10-10 18:55:19 +08:00
return _audioMgrObj.PlayAudio(key, type, pos, loop, isFade);
2024-07-25 17:43:14 +08:00
}
#endregion
2024-07-25 16:23:17 +08:00
public async UniTask<int> PlayAudioOld(string key, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound, GameObject posObj = null)
{
// if (DisableAudio) return 0;
// return await _audioMgrObj.PlayAudio(key, type, posObj);
2024-10-28 16:26:20 +08:00
DebugUtil.Log("更换流式播放后Key值需要修改为String详情参考AudioSourceConfig");
2024-07-25 16:23:17 +08:00
return -1;
}
2023-10-19 15:00:19 +08:00
2024-07-25 16:23:17 +08:00
public async UniTask<int> PlayAudioOld(string key, Vector3 pos, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound)
{
2024-10-28 16:26:20 +08:00
DebugUtil.Log("更换流式播放后Key值需要修改为String详情参考AudioSourceConfig");
2024-07-25 16:23:17 +08:00
return -1;
// if (DisableAudio) return 0;
// return await _audioMgrObj.PlayAudio(key, type, pos);
}
public async UniTask<int> PlayAudioOld(int id, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound, GameObject posObj = null)
2023-10-19 15:00:19 +08:00
{
// if (DisableAudio) return 0;
// return await _audioMgrOldObj.PlayAudio(id, type, posObj);
2024-10-28 16:26:20 +08:00
DebugUtil.Log("更换流式播放后Key值需要修改为String详情参考AudioSourceConfig");
return -1;
2023-10-24 18:53:34 +08:00
}
public async UniTask<int> PlayAudioOld(int id, Vector3 pos, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound)
2023-10-19 15:00:19 +08:00
{
// if (DisableAudio) return 0;
// DebugUtil.Log("Test");
// return await _audioMgrOldObj.PlayAudio(id, type, pos);
2024-10-28 16:26:20 +08:00
DebugUtil.Log("更换流式播放后Key值需要修改为String详情参考AudioSourceConfig");
return -1;
2023-10-19 15:00:19 +08:00
}
2024-03-26 12:22:47 +08:00
2024-09-10 16:21:12 +08:00
public void StopByID(int id)
{
if (DisableAudio) return;
_audioMgrObj.StopByID(id);
}
2024-04-24 12:59:23 +08:00
public void StopMusicById(int id)
{
2024-04-28 16:18:27 +08:00
if (DisableAudio) return;
2024-04-24 12:59:23 +08:00
_audioMgrObj.StopMusicById(id);
}
2024-09-10 16:21:12 +08:00
public void StopSoundById(int id)
{
if (DisableAudio) return;
_audioMgrObj.StopSoundByID(id);
}
2024-07-01 17:23:05 +08:00
public void PauseBackgroundMusic()
{
//TODO 暂停背景音乐
_audioMgrObj?.StopAllMusic();
}
public void ResumeBackgroundMusic()
{
//TODO 恢复背景音乐
_audioMgrObj?.ResumeAllMusic();
}
/// <summary>
/// 注册音频分析器的对音频挂接的监听
/// </summary>
/// <returns></returns>
public CriAtomExOutputAnalyzer RegisterAnalyzer()//CriAtomExOutputAnalyzer criAtomExOutputAnalyzer
2024-03-26 12:22:47 +08:00
{
if (DisableAudio) return null;
return _audioMgrObj.RegisterAnalyzer();//criAtomExOutputAnalyzer
2024-03-26 12:22:47 +08:00
}
/// <summary>
/// 取消音频分析器对挂接的监听
/// </summary>
public void UnregisterAnalyzer()//CriAtomExOutputAnalyzer criAtomExOutputAnalyzer
2024-03-26 12:22:47 +08:00
{
2024-04-28 16:18:27 +08:00
if (DisableAudio) return;
_audioMgrObj.UnregisterAnalyzer();//criAtomExOutputAnalyzer
2024-03-26 12:22:47 +08:00
}
2024-07-08 19:56:16 +08:00
public CriAtomExOutputAnalyzer GetAnalyzer()
{
return _audioMgrObj.analyzer;
}
2024-04-11 19:15:37 +08:00
public void StopAllSpeaks()
{
_audioMgrObj?.StopAllSpeaks();
}
public void StopAllMusic()
{
2024-10-17 16:24:24 +08:00
IsPlayingAlbum = false;
IsPlayingBGM = false;
2024-10-17 16:24:24 +08:00
_audioMgrObj?.StopAllMusic();
}
2024-04-11 19:15:37 +08:00
public void StopAll()
{
2024-10-17 16:24:24 +08:00
IsPlayingAlbum = false;
IsPlayingBGM = false;
2024-04-11 19:15:37 +08:00
_audioMgrObj.StopAll();
}
2024-04-23 19:42:31 +08:00
// public Audio GetAudioByID(AudioType audioType, int id)
// {
// if (DisableAudio) return null;
// return _audioMgrObj.GetAudioByID(audioType, id);
// }
public AudioInst GetAudioByID(AudioCriManager.EPlayType audioType, int id)
2024-04-23 19:42:31 +08:00
{
2024-04-28 16:18:27 +08:00
if (DisableAudio) return null;
2024-04-23 19:42:31 +08:00
return _audioMgrObj.GetAudioByID(audioType, id);
}
2024-08-01 14:03:20 +08:00
public AudioInst GetStoryAudioInst()
{
return _audioMgrObj.GetStoryAudioInst();
}
public void ReleaseStoryAudios()
{
_audioMgrObj.ReleaseStoryAudios();
}
public void PlayByCueName(int audioID, string cueName, bool isFade = false)
2024-08-01 14:03:20 +08:00
{
_audioMgrObj.PlayByCueName(audioID, cueName);
}
public void PlayByCueNameWithFade()
{
}
public void ReleaseAllAudios()
{
_audioMgrObj.ReleaseAllAudios();
}
2024-09-10 16:21:12 +08:00
public long GetCueLength(string cueSheet, string cueName)
{
return _audioMgrObj.GetCueLength(cueSheet, cueName);
}
2024-10-16 16:03:13 +08:00
public void InitLimit(int c0, int c1, int c2)
{
AudioMgrObj.InitLimit(c0, c1, c2);
}
}