AudioSourcePool.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using Fort23.Core;
  3. using UnityEngine;
  4. namespace Core.Audio
  5. {
  6. public class AudioSourcePool : IDisposable
  7. {
  8. public object TimeLineAudioEventLogic;
  9. private AudioSource _audioSource;
  10. private TimerEntity _timerEntity;
  11. // private AssetHandle _assetHandle;
  12. public bool IsPlay;
  13. public System.Action OnFinish;
  14. private float maxTime;
  15. public string CurrPlayName
  16. {
  17. get { return _currPlayName; }
  18. }
  19. public string _currPlayName;
  20. public float AudioVolume
  21. {
  22. get { return _audioVolume; }
  23. }
  24. private float _audioVolume;
  25. public bool IsBgm;
  26. // public float p
  27. public void Init(Transform root)
  28. {
  29. GameObject gameObject = new GameObject();
  30. _audioSource = gameObject.AddComponent<AudioSource>();
  31. gameObject.transform.SetParent(root);
  32. }
  33. public void Play(string audioName, bool isBgm, AssetHandle audioClip, bool isLoop, float volume,
  34. float speed = 1)
  35. {
  36. if (audioClip == null)
  37. {
  38. return;
  39. }
  40. // if (_assetHandle != null)
  41. // {
  42. // _assetHandle.Release();
  43. // }
  44. this.IsBgm = isBgm;
  45. IsPlay = true;
  46. _currPlayName = audioName;
  47. // _assetHandle = audioClip;
  48. _audioSource.clip = audioClip.AssetObject<AudioClip>();
  49. _audioSource.loop = isLoop;
  50. _audioSource.pitch = speed;
  51. _audioVolume = volume;
  52. Setvolume();
  53. if (speed > 1)
  54. {
  55. _audioSource.outputAudioMixerGroup = AudioManager.Instance.AudioMixerGroup;
  56. _audioSource.outputAudioMixerGroup.audioMixer.SetFloat("path", 1f / speed);
  57. }
  58. else
  59. {
  60. _audioSource.outputAudioMixerGroup = null;
  61. }
  62. _audioSource.Play();
  63. maxTime = _audioSource.clip.length;
  64. if (!isLoop)
  65. {
  66. _timerEntity = TimerComponent.Instance.AddTimer((long)(maxTime * 1000L),
  67. delegate { Finish(); });
  68. }
  69. }
  70. public float GetPlayTime()
  71. {
  72. if (_audioSource == null || _audioSource.clip == null)
  73. {
  74. return -1;
  75. }
  76. return _audioSource.time;
  77. }
  78. public void Setvolume(float volume)
  79. {
  80. _audioVolume = volume;
  81. if (_audioVolume < 0)
  82. {
  83. return;
  84. }
  85. Setvolume();
  86. }
  87. public void Setvolume()
  88. {
  89. _audioSource.volume = _audioVolume *
  90. (IsBgm ? AudioManager.Instance.BgmVolume : AudioManager.Instance.AudionVolume);
  91. }
  92. public float Progress()
  93. {
  94. return _audioSource.time / maxTime;
  95. }
  96. public void Finish()
  97. {
  98. IsPlay = false;
  99. OnFinish?.Invoke();
  100. _audioSource.Stop();
  101. AudioManager.Instance.Recycle(this);
  102. TimerComponent.Instance.Remove(_timerEntity);
  103. _timerEntity = null;
  104. }
  105. public void Stop()
  106. {
  107. IsPlay = false;
  108. _audioSource.Stop();
  109. }
  110. public void Pause()
  111. {
  112. _audioSource.Pause();
  113. }
  114. public void UnPause()
  115. {
  116. _audioSource.UnPause();
  117. }
  118. public void Dispose()
  119. {
  120. }
  121. }
  122. }