1376 lines
36 KiB
C#
1376 lines
36 KiB
C#
//流式播放的音频管理器
|
||
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using cfg.DeviceCfg;
|
||
using CriWare;
|
||
using Framework;
|
||
using PhxhSDK;
|
||
using UnityEngine;
|
||
using AudioType = PhxhSDK.AudioBase.AudioType;
|
||
using Object = UnityEngine.Object;
|
||
|
||
public class AudioCriManager: Singlenton<AudioCriManager>, IStart, IInitable, IUpdatable, IAudioPlay
|
||
{
|
||
public GameObject initGameObject;
|
||
public GameObject AudioRoot;
|
||
public GameObject CriAtomRoot;
|
||
public CriAtom CriAtom;
|
||
public ClipType curClipType;
|
||
|
||
private AudioInst _bgmAudio;
|
||
public CriAtomListener Listener;
|
||
private Dictionary<string, CriAtomEx.CueInfo[]> _cacheCueInfoArrays = new();
|
||
|
||
/// <summary>
|
||
/// 对象池数量限制
|
||
/// </summary>
|
||
public int MusicPoolLimit = 1;
|
||
public int SoundPoolLimit = 100;
|
||
public int SpeakPoolLimit = 1;
|
||
|
||
public enum EPlayType
|
||
{
|
||
Music,
|
||
Sound,
|
||
Speak
|
||
}
|
||
|
||
private GameObject _criError;
|
||
|
||
private Dictionary<int, AudioInst> _soundAudioDic;
|
||
private Dictionary<int, AudioInst> _soundAudioLoopDic;
|
||
private Dictionary<int, AudioInst> _musicAudioDic;
|
||
private Dictionary<int, AudioInst> _musicAudioLoopDic;
|
||
private Dictionary<int, AudioInst> _allAudioDic;
|
||
|
||
private List<GameObject> _soundAudioObj;
|
||
private List<GameObject> _soundAudioLoopObj;
|
||
private List<GameObject> _musicAudioObj;
|
||
private List<GameObject> _musicAudioLoopObj;
|
||
|
||
public enum PlayType
|
||
{
|
||
Music,
|
||
Sound,
|
||
Speak
|
||
}
|
||
|
||
private Dictionary<PlayType, List<int>> _playingLDic;
|
||
|
||
private List<AudioInst> _soundAudioQue;
|
||
|
||
private Dictionary<int, AudioInst> _storyAudioDic;
|
||
|
||
private const int SOUND_MAX_NUM = 20;
|
||
private const int MUSIC_MAX_NUM = 2;
|
||
|
||
#region Property
|
||
|
||
private float _vol = 1f;
|
||
|
||
public float GlobalSoundsVolume
|
||
{
|
||
get => AudioCri.GlobalSoundVolume;
|
||
set => AudioCri.GlobalSoundVolume = value;
|
||
}
|
||
public float GlobalSpeakVolume{
|
||
get => AudioCri.GlobalSpeakVolume;
|
||
set => AudioCri.GlobalSpeakVolume = value;
|
||
}
|
||
|
||
public float GlobalMusicVolume {
|
||
get => AudioCri.GlobalMusicVolume;
|
||
set => AudioCri.GlobalMusicVolume = value;
|
||
}
|
||
|
||
public float GlobalVolume
|
||
{
|
||
get => AudioCri.GlobalVolume;
|
||
set => AudioCri.GlobalVolume = value;
|
||
}
|
||
|
||
public bool GlobalIsOpenSpeak;
|
||
|
||
public AudioInst BgmAudio
|
||
{
|
||
get { return _bgmAudio; }
|
||
set { _bgmAudio = value; }
|
||
}
|
||
|
||
public CriAtomExOutputAnalyzer analyzer=null;
|
||
public bool IsAnalyzer = false;
|
||
|
||
|
||
#endregion
|
||
|
||
#region Analyze
|
||
|
||
//在播放之前注册
|
||
public CriAtomExOutputAnalyzer RegisterAnalyzer()//CriAtomExOutputAnalyzer s
|
||
{
|
||
IsAnalyzer = true;
|
||
if (analyzer == null)
|
||
{
|
||
analyzer = new(NewAnalyzerConfig());
|
||
}
|
||
return analyzer;
|
||
//analyzer = s;
|
||
}
|
||
|
||
public void UnregisterAnalyzer()//CriAtomExOutputAnalyzer s
|
||
{
|
||
//s.DetachExPlayer();
|
||
IsAnalyzer = false;
|
||
//analyzer = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取音频输出分析器配置
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private CriAtomExOutputAnalyzer.Config NewAnalyzerConfig()
|
||
{
|
||
CriAtomExOutputAnalyzer.Config config = new()
|
||
{
|
||
enableSpectrumAnalyzer = true, // 开启频谱分析
|
||
numSpectrumAnalyzerBands = 16, // 频谱分析数量SPECTRUM_SAMPLES_NUMBER
|
||
};
|
||
|
||
return config;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Framework
|
||
|
||
public void Start()
|
||
{
|
||
if (!AudioRoot)
|
||
{
|
||
curClipType = ClipType.CriwareClip;
|
||
|
||
AudioRoot = new GameObject();
|
||
AudioRoot.name = "Audio(Singleton)";
|
||
Object.DontDestroyOnLoad(AudioRoot);
|
||
|
||
if (curClipType == ClipType.CriwareClip)
|
||
{
|
||
_criError = new GameObject();
|
||
_criError.name = "CriwareError";
|
||
_criError.AddComponent<CriWareErrorHandler>();
|
||
Object.DontDestroyOnLoad(_criError);
|
||
}
|
||
|
||
var initialize = GameObject.Find("CriWareLibraryInitializer");
|
||
Object.DontDestroyOnLoad(initialize);
|
||
CriAtomRoot = GameObject.Find("CriAtom");
|
||
CriAtom = CriAtomRoot.GetComponent<CriAtom>();
|
||
Object.DontDestroyOnLoad(CriAtomRoot);
|
||
}
|
||
// CriAtomRoot = new GameObject()
|
||
// {
|
||
// name = "CriAtom"
|
||
// };
|
||
// CriAtom = CriAtomRoot.AddComponent<CriAtom>();
|
||
// CriAtom.AddCueSheet()
|
||
|
||
// for (int i = 0; i < SOUND_MAX_NUM; i++)
|
||
// {
|
||
// var go = new GameObject
|
||
// {
|
||
// name = "Sound" + (i+1)
|
||
// };
|
||
// go.transform.SetParent(AudioRoot.transform);
|
||
// }
|
||
//
|
||
// for (int i = 0; i < MUSIC_MAX_NUM; i++)
|
||
// {
|
||
// var go = new GameObject
|
||
// {
|
||
// name = "Music" + (i+1)
|
||
// };
|
||
// go.transform.SetParent(AudioRoot.transform);
|
||
// }
|
||
}
|
||
|
||
public void Init()
|
||
{
|
||
_soundAudioDic = new Dictionary<int, AudioInst>();
|
||
_soundAudioLoopDic = new Dictionary<int, AudioInst>();
|
||
_musicAudioDic = new Dictionary<int, AudioInst>();
|
||
_musicAudioLoopDic = new Dictionary<int, AudioInst>();
|
||
|
||
_allAudioDic = new Dictionary<int, AudioInst>();
|
||
_soundAudioLoopObj = new();
|
||
_musicAudioLoopObj = new();
|
||
_soundAudioObj = new();
|
||
_musicAudioObj = new();
|
||
_playingLDic = new ();
|
||
_playingLDic.Add(PlayType.Music, new List<int>());
|
||
_playingLDic.Add(PlayType.Sound, new List<int>());
|
||
_playingLDic.Add(PlayType.Speak, new List<int>());
|
||
|
||
_storyAudioDic = new();
|
||
}
|
||
|
||
public void Release()
|
||
{
|
||
|
||
}
|
||
|
||
public void Update(float dt)
|
||
{
|
||
foreach (var kv in _musicAudioDic)
|
||
{
|
||
var audio = kv.Value;
|
||
if (!audio.IsStopped)
|
||
{
|
||
audio.Update(dt);
|
||
}
|
||
}
|
||
|
||
foreach (var kv in _musicAudioLoopDic)
|
||
{
|
||
var audio = kv.Value;
|
||
if (!audio.IsStopped)
|
||
{
|
||
audio.Update(dt);
|
||
}
|
||
}
|
||
|
||
foreach (var kv in _soundAudioDic)
|
||
{
|
||
var audio = kv.Value;
|
||
if (!audio.IsStopped)
|
||
{
|
||
audio.Update(dt);
|
||
}
|
||
}
|
||
|
||
foreach (var kv in _soundAudioLoopDic)
|
||
{
|
||
var audio = kv.Value;
|
||
if (!audio.IsStopped)
|
||
{
|
||
audio.Update(dt);
|
||
}
|
||
}
|
||
|
||
foreach (var kv in _storyAudioDic)
|
||
{
|
||
var audio = kv.Value;
|
||
if (!audio.IsStopped)
|
||
{
|
||
audio.Update(dt);
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region LoadAsset
|
||
|
||
// private string _GetCriPath(string name)
|
||
// {
|
||
// var commonStr = "CueSheet_";
|
||
// var strArr = name.Split('_');
|
||
// var pre = strArr[0];
|
||
// if (pre != null)
|
||
// {
|
||
// if (pre == "Voice")
|
||
// {
|
||
// return "CueSheet_Voice";
|
||
// }
|
||
//
|
||
// if (pre == "bgm")
|
||
// {
|
||
// return "CueSheet_bgm";
|
||
// }
|
||
//
|
||
// if (pre == "Sound")
|
||
// {
|
||
// return "CueSheet_Sound";
|
||
// }
|
||
//
|
||
// if (pre == "ev")
|
||
// {
|
||
// return "CueSheet_ev";
|
||
// }
|
||
//
|
||
// if (pre == "ui")
|
||
// {
|
||
// return "CueSheet_ui";
|
||
// }
|
||
// }
|
||
//
|
||
// return "";
|
||
// }
|
||
|
||
//优化SplitGC
|
||
private string _GetCriPath(string name)
|
||
{
|
||
var firstChar = name[0];
|
||
if (!string.IsNullOrEmpty(name))
|
||
{
|
||
if (firstChar == 'b')
|
||
{
|
||
return "CueSheet_bgm";
|
||
}
|
||
|
||
if (firstChar == 'V')
|
||
{
|
||
return "CueSheet_Voice";
|
||
}
|
||
|
||
if (firstChar == 'S')
|
||
{
|
||
return "CueSheet_Sound";
|
||
}
|
||
|
||
if (firstChar == 'e')
|
||
{
|
||
return "CueSheet_ev";
|
||
}
|
||
|
||
if (firstChar == 'u')
|
||
{
|
||
return "CueSheet_ui";
|
||
}
|
||
}
|
||
|
||
return "";
|
||
}
|
||
|
||
private string _GetAcbPath(string name)
|
||
{
|
||
var path = _GetCriPath(name);
|
||
if (string.IsNullOrEmpty(path))
|
||
{
|
||
DebugUtil.LogError($"获取不到对应的ACB文件,key: {name}");
|
||
return "";
|
||
}
|
||
|
||
return path + ".acb";
|
||
}
|
||
|
||
private string _GetAwbPath(string name)
|
||
{
|
||
var path = _GetCriPath(name);
|
||
if (string.IsNullOrEmpty(path))
|
||
{
|
||
DebugUtil.LogError($"获取不到对应的AWB文件,key: {name}");
|
||
return "";
|
||
}
|
||
|
||
return path + ".awb";
|
||
}
|
||
|
||
private string _GetAcfPath(string name)
|
||
{
|
||
var path = _GetCriPath(name);
|
||
if (path == null)
|
||
{
|
||
DebugUtil.LogError($"获取不到对应的ACF文件,key: {name}");
|
||
return "";
|
||
}
|
||
|
||
return path + ".acf";
|
||
}
|
||
#endregion
|
||
|
||
#region AudioObj
|
||
|
||
/// <summary>
|
||
/// 获取可用的Audio
|
||
/// </summary>
|
||
/// <param name="audioType"></param>
|
||
/// <returns></returns>
|
||
private AudioInst _GetUsableAudio(AudioBase.AudioType audioType, bool isLoop, bool isSpeak = false)
|
||
{
|
||
var limit = _CheckIsAudioLimited(audioType, isSpeak);
|
||
if (limit)
|
||
{
|
||
return _GetOldAudio(audioType, isLoop, isSpeak);
|
||
}
|
||
var freeAudio = _GetFreeAudio(audioType, isLoop);
|
||
return freeAudio?? _CreateNewAudio(audioType, isLoop);
|
||
}
|
||
|
||
private AudioInst _GetOldAudio(AudioBase.AudioType audioType, bool isLoop, bool isSpeak)
|
||
{
|
||
var playingList = _playingLDic[GetPlayType(audioType, isSpeak)];
|
||
if (playingList.Count > 0)
|
||
{
|
||
var audio = GetAudioByID(playingList[0]);
|
||
return audio;
|
||
}
|
||
return _CreateNewAudio(audioType, isLoop);
|
||
}
|
||
|
||
private int _GetLimit(AudioBase.AudioType audioType, bool isSpeak)
|
||
{
|
||
if (isSpeak)
|
||
{
|
||
return SpeakPoolLimit;
|
||
}
|
||
|
||
if (audioType == AudioBase.AudioType.Music)
|
||
{
|
||
return MusicPoolLimit;
|
||
}
|
||
|
||
if (audioType == AudioBase.AudioType.Sound)
|
||
{
|
||
return SoundPoolLimit;
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
private PlayType GetPlayType(AudioBase.AudioType audioType, bool isSpeak)
|
||
{
|
||
if (isSpeak)
|
||
return PlayType.Speak;
|
||
if (audioType == AudioBase.AudioType.Music)
|
||
return PlayType.Music;
|
||
return PlayType.Sound;
|
||
}
|
||
private bool _CheckIsAudioLimited(AudioBase.AudioType audioType, bool isSpeak)
|
||
{
|
||
var limit = _GetLimit(audioType, isSpeak);
|
||
var playingList = _playingLDic[GetPlayType(audioType, isSpeak)];
|
||
if (playingList.Count >= limit)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
private Dictionary<int, AudioInst> _GetAudioDic(AudioBase.AudioType audioType, bool isLoop)
|
||
{
|
||
switch (audioType)
|
||
{
|
||
case AudioBase.AudioType.Music:
|
||
return isLoop? _musicAudioLoopDic : _musicAudioDic;
|
||
case AudioBase.AudioType.Sound:
|
||
return isLoop? _soundAudioLoopDic : _soundAudioDic;
|
||
default:
|
||
{
|
||
DebugUtil.LogError("获取AudioDic出错!!!传入AudioType有误!");
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
|
||
private AudioInst _GetFreeAudio(AudioBase.AudioType audioType, bool isLoop)
|
||
{
|
||
var dic = _GetAudioDic(audioType, isLoop);
|
||
if (dic != null)
|
||
{
|
||
foreach (var kv in dic)
|
||
{
|
||
var audio = kv.Value;
|
||
if (audio.IsStopped)
|
||
{
|
||
return audio;
|
||
}
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
private AudioInst _CreateStoryAudio(GameObject posGo = null)
|
||
{
|
||
var rootGo = new GameObject()
|
||
{
|
||
name = "AudioStory"
|
||
};
|
||
rootGo.transform.SetParent(AudioRoot.transform);
|
||
var audio = new AudioInst(AudioBase.AudioType.Sound, rootGo, null);
|
||
rootGo.name += audio.audioID;
|
||
_storyAudioDic.TryAdd(audio.audioID, audio);
|
||
_allAudioDic.Add(audio.audioID, audio);
|
||
return audio;
|
||
}
|
||
|
||
private AudioInst _CreateNewAudio(AudioBase.AudioType audioType, bool isLoop)
|
||
{
|
||
var rootGo = new GameObject()
|
||
{
|
||
name = "Audio"
|
||
};
|
||
rootGo.transform.SetParent(AudioRoot.transform);
|
||
var audio = new AudioInst(audioType, rootGo, null);
|
||
|
||
if (audioType == AudioBase.AudioType.Music)
|
||
{
|
||
rootGo.name = "music";
|
||
rootGo.name += audio.audioID;
|
||
if (isLoop)
|
||
{
|
||
rootGo.name += "Loop";
|
||
_musicAudioLoopDic.TryAdd(audio.audioID, audio);
|
||
_musicAudioLoopObj.Add(rootGo);
|
||
}
|
||
else
|
||
{
|
||
_musicAudioDic.TryAdd(audio.audioID, audio);
|
||
_musicAudioObj.Add(rootGo);
|
||
}
|
||
|
||
if (IsAnalyzer)
|
||
{
|
||
rootGo.GetComponent<CriAtomSource>().AttachToAnalyzer(analyzer);
|
||
}
|
||
}
|
||
if (audioType == AudioBase.AudioType.Sound)
|
||
{
|
||
rootGo.name = "Sound";
|
||
rootGo.name += audio.audioID;
|
||
if (isLoop)
|
||
{
|
||
rootGo.name += "Loop";
|
||
_soundAudioLoopDic.TryAdd(audio.audioID, audio);
|
||
_soundAudioLoopObj.Add(rootGo);
|
||
}
|
||
else
|
||
{
|
||
_soundAudioDic.TryAdd(audio.audioID, audio);
|
||
_soundAudioObj.Add(rootGo);
|
||
}
|
||
}
|
||
_allAudioDic.Add(audio.audioID, audio);
|
||
|
||
return audio;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Play
|
||
|
||
/// <summary>
|
||
/// 调用接口,传obj
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <param name="type"></param>
|
||
/// <param name="posObj"></param>
|
||
/// <returns></returns>
|
||
private int _PlayAudio(string key, EPlayType type, GameObject posObj = null, bool isLoop = false,
|
||
bool isFade = false)
|
||
{
|
||
if (string.IsNullOrEmpty(key))
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
//策略2 超过限制不播
|
||
// var audioType = AudioBase.AudioType.Music;
|
||
// bool isSpeak = false;
|
||
// if (type == EPlayType.Sound)
|
||
// {
|
||
// audioType = AudioBase.AudioType.Sound;
|
||
// }
|
||
// if (type == EPlayType.Speak)
|
||
// {
|
||
// audioType = AudioBase.AudioType.Sound;
|
||
// isSpeak = true;
|
||
// }
|
||
// if (_CheckIsAudioLimited(audioType, isSpeak))
|
||
// {
|
||
// return -1;
|
||
// }
|
||
|
||
if (CriAtom)
|
||
{
|
||
var cueSheetName = _GetCriPath(key);
|
||
if (!string.IsNullOrEmpty(cueSheetName))
|
||
{
|
||
if (CriAtom.GetCueSheetInternal(cueSheetName) == null)
|
||
{
|
||
var acbPath = _GetAcbPath(key);
|
||
var awbPath = _GetAwbPath(key);
|
||
if (string.IsNullOrEmpty(acbPath) || string.IsNullOrEmpty(awbPath))
|
||
{
|
||
return -1;
|
||
}
|
||
var cueSheet = CriAtom.AddCueSheet(cueSheetName, acbPath, awbPath, null);
|
||
}
|
||
}
|
||
|
||
var criCueSheet = CriAtom.GetCueSheetInternal(cueSheetName);
|
||
if (criCueSheet != null && criCueSheet.acb != null)
|
||
{
|
||
var cueInfoList = CriAtom.GetCueSheetInternal(cueSheetName).acb.GetCueInfoList();
|
||
var cueInfo = Array.Find(cueInfoList, (cue) => cue.name == key );
|
||
if (cueInfo.name == null)
|
||
{
|
||
DebugUtil.LogWarning($"Can not Find Criware Key:{key}");
|
||
return -1;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogWarning($"Can not Find Criware Key:{key}");
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
string path = _GetCriPath(key);
|
||
|
||
switch (type)
|
||
{
|
||
case EPlayType.Music:
|
||
{
|
||
return _PlayMusic(key, path, posObj, isLoop, isFade);
|
||
}
|
||
case EPlayType.Sound:
|
||
{
|
||
return _PlaySound(key, path, posObj, isLoop, isFade);
|
||
}
|
||
case EPlayType.Speak:
|
||
{
|
||
return _PlaySpeakSound(key, path, posObj, isFade);
|
||
}
|
||
default: break;
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调用接口,传位置
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <param name="type"></param>
|
||
/// <param name="pos"></param>
|
||
/// <returns></returns>
|
||
private int _PlayAudio(string key, EPlayType type, Vector3 pos, bool loop = false,
|
||
bool isFade = false)
|
||
{
|
||
//策略2 超过限制不播
|
||
// var audioType = AudioBase.AudioType.Music;
|
||
// bool isSpeak = false;
|
||
// if (type == EPlayType.Sound)
|
||
// {
|
||
// audioType = AudioBase.AudioType.Sound;
|
||
// }
|
||
// if (type == EPlayType.Speak)
|
||
// {
|
||
// audioType = AudioBase.AudioType.Sound;
|
||
// isSpeak = true;
|
||
// }
|
||
// if (_CheckIsAudioLimited(audioType, isSpeak))
|
||
// {
|
||
// return -1;
|
||
// }
|
||
|
||
if (CriAtom)
|
||
{
|
||
var cueSheetName = _GetCriPath(key);
|
||
if (CriAtom.GetCueSheetInternal(cueSheetName) == null)
|
||
{
|
||
var acbPath = _GetAcbPath(key);
|
||
var awbPath = _GetAwbPath(key);
|
||
if (string.IsNullOrEmpty(acbPath) || string.IsNullOrEmpty(awbPath))
|
||
{
|
||
return -1;
|
||
}
|
||
var cueSheet = CriAtom.AddCueSheet(cueSheetName, acbPath, awbPath, null);
|
||
}
|
||
|
||
CriAtomEx.CueInfo[] cueInfos;
|
||
if (!_cacheCueInfoArrays.ContainsKey(cueSheetName))
|
||
{
|
||
cueInfos = CriAtom.GetCueSheetInternal(cueSheetName).acb.GetCueInfoList();
|
||
_cacheCueInfoArrays.Add(cueSheetName, cueInfos);
|
||
}
|
||
else
|
||
{
|
||
cueInfos = _cacheCueInfoArrays[cueSheetName];
|
||
}
|
||
// var cueInfoList = CriAtom.GetCueSheetInternal(cueSheetName).acb.GetCueInfoList();
|
||
var cueInfo = Array.Find(cueInfos, (cue) => cue.name == key );
|
||
if (cueInfo.name == null)
|
||
{
|
||
DebugUtil.LogWarning($"Can not Find Criware Key:{key}");
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
string path = _GetCriPath(key);
|
||
|
||
switch (type)
|
||
{
|
||
case EPlayType.Music:
|
||
{
|
||
return _PlayMusic(key, path, pos, loop);
|
||
}
|
||
case EPlayType.Sound:
|
||
{
|
||
return _PlaySound(key, path, pos, loop, isFade);
|
||
}
|
||
case EPlayType.Speak:
|
||
{
|
||
return _PlaySpeakSound(key, path, pos);
|
||
}
|
||
default: break;
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
private int _PlayMusic(string key, string path, GameObject posObj = null, bool isLoop = true, bool isFade = false, float volume = 1f)
|
||
{
|
||
if (!isFade)
|
||
{
|
||
// if (_bgmAudio != null)
|
||
// {
|
||
// _bgmAudio.Stop();
|
||
// }
|
||
AudioInfo audioInfo = new AudioInfo()
|
||
{
|
||
persist = true,
|
||
volume = 1,
|
||
fadeInValue = 0.3f,
|
||
fadeOutValue = 0.3f,
|
||
};
|
||
var audio = _GetUsableAudio(AudioBase.AudioType.Music, isLoop);
|
||
audio.SetPosObj(posObj);
|
||
audio.SetIsSpeak(false);
|
||
audio.SetVolume(volume);
|
||
audio.SetIsLoop(isLoop);
|
||
// if (_bgmAudio != null && _bgmAudio != audio)
|
||
// {
|
||
// _bgmAudio.Stop();
|
||
// }
|
||
audio.Stop();
|
||
audio.Play(path, key);
|
||
audio.SetIsLoop(isLoop);
|
||
//_bgmAudio = audio;
|
||
return audio.audioID;
|
||
}
|
||
else
|
||
{
|
||
AudioInfo audioInfo = new AudioInfo()
|
||
{
|
||
persist = true,
|
||
volume = 1,
|
||
fadeInValue = 0.3f,
|
||
fadeOutValue = 0.3f,
|
||
};
|
||
var audio = _GetUsableAudio(AudioBase.AudioType.Music, isLoop);
|
||
audio.SetPosObj(posObj);
|
||
audio.SetIsSpeak(false);
|
||
// if (_bgmAudio != null && _bgmAudio != audio)
|
||
// {
|
||
// _bgmAudio.Stop();
|
||
// }
|
||
|
||
//audio.SetIsFadeIn(true);
|
||
audio.Stop();
|
||
audio.PlayWithFade(path, key);
|
||
audio.SetIsLoop(isLoop);
|
||
//_bgmAudio = audio;
|
||
return audio.audioID;
|
||
}
|
||
}
|
||
|
||
private int _PlayMusic(string key, string path, Vector3 pos, bool isLoop, bool isFade = false, float volume = 1f)
|
||
{
|
||
AudioInfo audioInfo = new AudioInfo()
|
||
{
|
||
persist = true,
|
||
volume = 1,
|
||
fadeInValue = 0.3f,
|
||
fadeOutValue = 0.3f,
|
||
};
|
||
var audio = _GetUsableAudio(AudioBase.AudioType.Music, isLoop);
|
||
audio.SetPos(pos);
|
||
audio.SetIsSpeak(false);
|
||
|
||
// if (_bgmAudio != null && _bgmAudio != audio)
|
||
// {
|
||
// _bgmAudio.Stop();
|
||
// }
|
||
|
||
audio.SetIsLoop(true);
|
||
audio.Stop();
|
||
if (isFade)
|
||
{
|
||
audio.PlayWithFade(path, key);
|
||
}
|
||
else
|
||
{
|
||
audio.Play(path, key);
|
||
}
|
||
|
||
//_bgmAudio = audio;
|
||
|
||
audio.SetIsLoop(isLoop);
|
||
return audio.audioID;
|
||
}
|
||
|
||
private int _PlaySound(string key, string path, GameObject posObj = null, bool isLoop = false,
|
||
bool isFade = false, float volume = 1f)
|
||
{
|
||
AudioInfo audioInfo = new AudioInfo()
|
||
{
|
||
persist = true,
|
||
volume = 1,
|
||
fadeInValue = 0.3f,
|
||
fadeOutValue = 0.3f,
|
||
};
|
||
|
||
var audio = _GetUsableAudio(AudioBase.AudioType.Sound, isLoop);
|
||
audio.Stop();
|
||
|
||
audio.SetPosObj(posObj);
|
||
audio.SetIsSpeak(false);
|
||
audio.SetIsLoop(isLoop);
|
||
if (posObj)
|
||
{
|
||
audio.SetIs3D(true);
|
||
}
|
||
|
||
if (isFade)
|
||
{
|
||
audio.SetVolume(0);
|
||
audio.PlayWithFade(path, key);
|
||
}
|
||
else
|
||
{
|
||
audio.SetVolume(volume);
|
||
audio.Play(path, key);
|
||
}
|
||
|
||
audio.SetIsLoop(isLoop);
|
||
return audio.audioID;
|
||
}
|
||
|
||
private int _PlaySound(string key, string path, Vector3 pos, bool isLoop = false, bool isFade = false, float volume = 1f)
|
||
{
|
||
AudioInfo audioInfo = new AudioInfo()
|
||
{
|
||
persist = true,
|
||
volume = 1,
|
||
fadeInValue = 0.3f,
|
||
fadeOutValue = 0.3f,
|
||
};
|
||
var audio = _GetUsableAudio(AudioBase.AudioType.Sound, isLoop);
|
||
audio.Stop();
|
||
|
||
audio.SetPos(pos);
|
||
audio.SetIsLoop(isLoop);
|
||
audio.SetIsSpeak(false);
|
||
audio.SetIs3D(true);
|
||
if (isFade)
|
||
{
|
||
audio.SetVolume(0);
|
||
audio.PlayWithFade(path, key);
|
||
}
|
||
else
|
||
{
|
||
audio.SetVolume(volume);
|
||
audio.Play(path, key);
|
||
}
|
||
|
||
audio.SetIsLoop(isLoop);
|
||
return audio.audioID;
|
||
}
|
||
|
||
private int _PlaySpeakSound(string key, string path, GameObject posObj = null, bool isFade = false, float volume = 1f)
|
||
{
|
||
AudioInfo audioInfo = new AudioInfo()
|
||
{
|
||
persist = true,
|
||
volume = 1,
|
||
fadeInValue = 0.3f,
|
||
fadeOutValue = 0.3f,
|
||
};
|
||
|
||
var audio = _GetUsableAudio(AudioBase.AudioType.Sound, false);
|
||
audio.SetPosObj(posObj);
|
||
audio.SetIsSpeak(true);
|
||
|
||
if (isFade)
|
||
{
|
||
audio.SetVolume(0);
|
||
audio.PlayWithFade(path, key);
|
||
}
|
||
else
|
||
{
|
||
audio.SetVolume(volume);
|
||
audio.Play(path, key);
|
||
}
|
||
audio.SetIsLoop(false);
|
||
|
||
return audio.audioID;
|
||
}
|
||
|
||
private int _PlaySpeakSound(string key, string path, Vector3 pos, bool isFade = false, float volume = 1f)
|
||
{
|
||
AudioInfo audioInfo = new AudioInfo()
|
||
{
|
||
persist = true,
|
||
volume = 1,
|
||
fadeInValue = 0.3f,
|
||
fadeOutValue = 0.3f,
|
||
};
|
||
var audio = _GetUsableAudio(AudioBase.AudioType.Sound, false);
|
||
audio.SetPos(pos);
|
||
audio.SetIsSpeak(true);
|
||
if (isFade)
|
||
{
|
||
audio.SetVolume(0);
|
||
audio.PlayWithFade(path, key);
|
||
}
|
||
else
|
||
{
|
||
audio.SetVolume(volume);
|
||
audio.Play(path, key);
|
||
}
|
||
audio.SetIsLoop(false);
|
||
return audio.audioID;
|
||
}
|
||
|
||
|
||
private void _PlayAudioByID(int id, string key, EPlayType type, GameObject posObj = null)
|
||
{
|
||
if (_soundAudioDic.TryGetValue(id, out var audio))
|
||
{
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region API
|
||
|
||
public int PlayAudio(string key, EPlayType type, GameObject posObj = null, bool loop = false,
|
||
bool isFade = false)
|
||
{
|
||
#if DEVELOPMENT_BUILD || DEBUG
|
||
DebugUtil.Log("播放音频:{0}, type:{1}", key, type);
|
||
#endif
|
||
return _PlayAudio(key, type, posObj, loop, isFade);
|
||
}
|
||
|
||
public int PlayAudio(string key, EPlayType type, Vector3 pos, bool loop = false,
|
||
bool isFade = false)
|
||
{
|
||
#if DEVELOPMENT_BUILD || DEBUG
|
||
DebugUtil.Log("播放音频:{0}, type:{1}", key, type);
|
||
#endif
|
||
return _PlayAudio(key, type, pos, loop, isFade);
|
||
}
|
||
|
||
public int PlayAudio()
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
public void InitLimit(int count0, int count1, int count2)
|
||
{
|
||
MusicPoolLimit = count0;
|
||
SoundPoolLimit = count1;
|
||
SpeakPoolLimit = count2;
|
||
}
|
||
|
||
public AudioInst GetAudioByID(EPlayType audioType, int id)
|
||
{
|
||
if (audioType == EPlayType.Music)
|
||
{
|
||
if (_musicAudioDic.ContainsKey(id))
|
||
{
|
||
return _musicAudioDic[id];
|
||
}
|
||
|
||
if (_musicAudioLoopDic.ContainsKey(id))
|
||
{
|
||
return _musicAudioLoopDic[id];
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
if (audioType == EPlayType.Sound || audioType == EPlayType.Speak)
|
||
{
|
||
if (_soundAudioDic.ContainsKey(id))
|
||
{
|
||
return _soundAudioDic[id];
|
||
}
|
||
|
||
if (_soundAudioLoopDic.ContainsKey(id))
|
||
{
|
||
return _soundAudioLoopDic[id];
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public AudioInst GetAudioByID(int id)
|
||
{
|
||
if (_musicAudioDic.ContainsKey(id))
|
||
{
|
||
return _musicAudioDic[id];
|
||
}
|
||
|
||
if (_musicAudioLoopDic.ContainsKey(id))
|
||
{
|
||
return _musicAudioLoopDic[id];
|
||
}
|
||
|
||
if (_soundAudioDic.ContainsKey(id))
|
||
{
|
||
return _soundAudioDic[id];
|
||
}
|
||
|
||
if (_soundAudioLoopDic.ContainsKey(id))
|
||
{
|
||
return _soundAudioLoopDic[id];
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public void StopMusicById(int audioID)
|
||
{
|
||
if (audioID >= 0)
|
||
{
|
||
if(_musicAudioDic.TryGetValue(audioID, out var audio))
|
||
{
|
||
audio.Stop();
|
||
}
|
||
|
||
if(_musicAudioLoopDic.TryGetValue(audioID, out var audioLoop))
|
||
{
|
||
audioLoop.Stop();
|
||
}
|
||
}
|
||
}
|
||
|
||
public void StopSoundByID(int audioID)
|
||
{
|
||
if (audioID >= 0)
|
||
{
|
||
if(_soundAudioDic.TryGetValue(audioID, out var audio))
|
||
{
|
||
audio.Stop();
|
||
}
|
||
|
||
if(_soundAudioLoopDic.TryGetValue(audioID, out var audioLoop))
|
||
{
|
||
audioLoop.Stop();
|
||
}
|
||
}
|
||
}
|
||
|
||
public void StopByID(int audioID)
|
||
{
|
||
if (audioID >= 0)
|
||
{
|
||
if(_musicAudioDic.TryGetValue(audioID, out var audio))
|
||
{
|
||
audio.Stop();
|
||
}
|
||
|
||
if(_musicAudioLoopDic.TryGetValue(audioID, out var audioLoop))
|
||
{
|
||
audioLoop.Stop();
|
||
}
|
||
|
||
if (_soundAudioDic.TryGetValue(audioID, out var soundAudio))
|
||
{
|
||
soundAudio.Stop();
|
||
}
|
||
|
||
if(_soundAudioLoopDic.TryGetValue(audioID, out var audioLoop1))
|
||
{
|
||
audioLoop1.Stop();
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
public void StopAllMusic()
|
||
{
|
||
// if (_bgmAudio != null)
|
||
// {
|
||
// _bgmAudio.Stop();
|
||
// }
|
||
foreach (var kv in _musicAudioDic)
|
||
{
|
||
var audio = kv.Value;
|
||
audio.Stop();
|
||
}
|
||
|
||
foreach (var kv in _musicAudioLoopDic)
|
||
{
|
||
var audio = kv.Value;
|
||
audio.Stop();
|
||
}
|
||
}
|
||
|
||
public void StopAllSounds()
|
||
{
|
||
foreach (var kv in _soundAudioDic)
|
||
{
|
||
var audio = kv.Value;
|
||
audio.Stop();
|
||
}
|
||
|
||
foreach (var kv in _soundAudioLoopDic)
|
||
{
|
||
var audio = kv.Value;
|
||
audio.Stop();
|
||
}
|
||
}
|
||
|
||
public void StopAllSpeaks()
|
||
{
|
||
foreach (var kv in _soundAudioDic)
|
||
{
|
||
var audio = kv.Value;
|
||
if(audio.IsSpeak)
|
||
audio.Stop();
|
||
}
|
||
}
|
||
|
||
public void StopAll()
|
||
{
|
||
foreach (var kv in _allAudioDic)
|
||
{
|
||
var audio = kv.Value;
|
||
audio.Stop();
|
||
}
|
||
}
|
||
|
||
public void ResumeAllMusic()
|
||
{
|
||
|
||
// if (_bgmAudio != null)
|
||
// {
|
||
// _bgmAudio.Resume();
|
||
// }
|
||
foreach (var kv in _musicAudioDic)
|
||
{
|
||
var audio = kv.Value;
|
||
audio.Resume();
|
||
}
|
||
|
||
foreach (var kv in _musicAudioLoopDic)
|
||
{
|
||
var audio = kv.Value;
|
||
audio.Resume();
|
||
}
|
||
}
|
||
|
||
public void StopBGM()
|
||
{
|
||
_bgmAudio.Stop();
|
||
}
|
||
|
||
public void AfterSwitchMainCamera(Camera camera)
|
||
{
|
||
var listener = camera.GetComponent<CriAtomListener>();
|
||
if (!listener)
|
||
{
|
||
camera.gameObject.AddComponent<CriAtomListener>();
|
||
}
|
||
}
|
||
|
||
public CriAtomExAcb GetAcb(string cueSheetName)
|
||
{
|
||
return CriAtom.GetAcb(cueSheetName);
|
||
}
|
||
|
||
public AudioInst GetStoryAudioInst()
|
||
{
|
||
return _CreateStoryAudio();
|
||
}
|
||
|
||
public void ReleaseStoryAudios()
|
||
{
|
||
foreach (var kv in _storyAudioDic)
|
||
{
|
||
var audio = kv.Value;
|
||
audio.Stop();
|
||
Object.Destroy(audio.RootGo);
|
||
_allAudioDic.Remove(audio.audioID);
|
||
}
|
||
_storyAudioDic.Clear();
|
||
}
|
||
|
||
public void PlayByCueName(int audioID, string cueName, bool isFade = false)
|
||
{
|
||
if (_allAudioDic.TryGetValue(audioID, out var audio))
|
||
{
|
||
var cueSheet = _GetCriPath(cueName);
|
||
if (isFade)
|
||
{
|
||
audio.PlayWithFade(cueSheet, cueName);
|
||
}
|
||
else
|
||
{
|
||
audio.Play(cueSheet, cueName);
|
||
}
|
||
}
|
||
}
|
||
|
||
public void ReleaseMusicAudios()
|
||
{
|
||
var musicAudioList = _musicAudioDic.ToList();
|
||
for (int i = musicAudioList.Count - 1; i >= 0; i--)
|
||
{
|
||
var kv = musicAudioList[i];
|
||
var audio = kv.Value;
|
||
var audioID = kv.Key;
|
||
audio.Stop();
|
||
_musicAudioDic.Remove(audioID);
|
||
_allAudioDic.Remove(audioID);
|
||
Object.Destroy(audio.RootGo);
|
||
}
|
||
|
||
var musicAudioLoopList = _musicAudioLoopDic.ToList();
|
||
for (int i = musicAudioLoopList.Count - 1; i >= 0; i--)
|
||
{
|
||
var kv = musicAudioLoopList[i];
|
||
var audio = kv.Value;
|
||
var audioID = kv.Key;
|
||
audio.Stop();
|
||
_musicAudioLoopDic.Remove(audioID);
|
||
_allAudioDic.Remove(audioID);
|
||
Object.Destroy(audio.RootGo);
|
||
}
|
||
|
||
_musicAudioObj.Clear();
|
||
_musicAudioDic.Clear();
|
||
_musicAudioLoopObj.Clear();
|
||
_musicAudioLoopDic.Clear();
|
||
}
|
||
|
||
public void ReleaseSoundsAudios()
|
||
{
|
||
var soundAudioList = _soundAudioDic.ToList();
|
||
for (int i = soundAudioList.Count - 1; i >= 0; i--)
|
||
{
|
||
var kv = soundAudioList[i];
|
||
var audio = kv.Value;
|
||
var audioID = kv.Key;
|
||
audio.Stop();
|
||
_soundAudioDic.Remove(audioID);
|
||
_allAudioDic.Remove(audioID);
|
||
Object.Destroy(audio.RootGo);
|
||
}
|
||
|
||
var soundAudioLoopList = _soundAudioLoopDic.ToList();
|
||
for (int i = soundAudioLoopList.Count - 1; i >= 0; i--)
|
||
{
|
||
var kv = soundAudioLoopList[i];
|
||
var audio = kv.Value;
|
||
var audioID = kv.Key;
|
||
audio.Stop();
|
||
_soundAudioLoopDic.Remove(audioID);
|
||
_allAudioDic.Remove(audioID);
|
||
Object.Destroy(audio.RootGo);
|
||
}
|
||
|
||
_soundAudioObj.Clear();
|
||
_soundAudioDic.Clear();
|
||
_soundAudioLoopObj.Clear();
|
||
_soundAudioLoopDic.Clear();
|
||
}
|
||
|
||
public void ReleaseAllAudios()
|
||
{
|
||
ReleaseMusicAudios();
|
||
ReleaseSoundsAudios();
|
||
}
|
||
|
||
public long GetCueLength(string cueSheet, string cueName)
|
||
{
|
||
var acb = CriAtom.GetCueSheetInternal(cueSheet).acb;
|
||
if (acb.GetCueInfo(cueName, out var cueInfo))
|
||
{
|
||
var length = cueInfo.length / 1000;
|
||
return length;
|
||
}
|
||
|
||
return -1;
|
||
}
|
||
|
||
public void Add2Playing(AudioBase.AudioType audioType, int id, bool isSpeak = false)
|
||
{
|
||
PlayType type;
|
||
if (audioType == AudioBase.AudioType.Music)
|
||
{
|
||
type = PlayType.Music;
|
||
}
|
||
else
|
||
{
|
||
if (isSpeak)
|
||
type = PlayType.Speak;
|
||
else
|
||
{
|
||
type = PlayType.Sound;
|
||
}
|
||
}
|
||
var list = _playingLDic[type];
|
||
if (!list.Contains(id))
|
||
{
|
||
list.Add(id);
|
||
}
|
||
}
|
||
|
||
public void RemovePlaying(AudioBase.AudioType audioType, int id, bool isSpeak = false)
|
||
{
|
||
PlayType type;
|
||
if (audioType == AudioBase.AudioType.Music)
|
||
{
|
||
type = PlayType.Music;
|
||
}
|
||
else
|
||
{
|
||
if (isSpeak)
|
||
type = PlayType.Speak;
|
||
else
|
||
{
|
||
type = PlayType.Sound;
|
||
}
|
||
}
|
||
var list = _playingLDic[type];
|
||
if (list.Contains(id))
|
||
{
|
||
list.Remove(id);
|
||
}
|
||
}
|
||
|
||
public void RemovePlaying(int id)
|
||
{
|
||
foreach (var kv in _playingLDic)
|
||
{
|
||
var list = kv.Value;
|
||
if (list.Contains(id))
|
||
{
|
||
list.Remove(id);
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
|
||
public class AudioInst : AudioCri
|
||
{
|
||
public AudioInst(AudioType audioType, GameObject rootGo, GameObject posGo, AudioInfo audioInfo = new(),
|
||
bool loop = false, bool is3D = false):base(audioType, rootGo, posGo, audioInfo, loop, is3D)
|
||
{
|
||
|
||
}
|
||
} |