AudioManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /****************************************************************************
  2. * Copyright (c) 2017 snowcold
  3. * Copyright (c) 2017 ~ 2020.5 liangxie
  4. *
  5. * http://qframework.io
  6. * https://github.com/liangxiegame/QFramework
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. ****************************************************************************/
  26. using System;
  27. using System.Linq;
  28. namespace QFramework
  29. {
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. /// <summary>
  33. /// TODO:目前,不支持本地化
  34. /// </summary>
  35. [MonoSingletonPath("[Audio]/AudioManager")]
  36. public partial class AudioManager : MonoBehaviour, ISingleton
  37. {
  38. public AudioPlayer MusicPlayer { get; private set; }
  39. public AudioPlayer VoicePlayer { get; private set; }
  40. public void OnSingletonInit()
  41. {
  42. SafeObjectPool<AudioPlayer>.Instance.Init(10, 1);
  43. MusicPlayer = AudioPlayer.Allocate();
  44. MusicPlayer.usedCache = false;
  45. VoicePlayer = AudioPlayer.Allocate();
  46. VoicePlayer.usedCache = false;
  47. CheckAudioListener();
  48. gameObject.transform.position = Vector3.zero;
  49. AudioKit.Settings.MusicVolume.Bind(volume => { MusicPlayer.SetVolume(volume); })
  50. .DisposeWhenGameObjectDestroyed(this);
  51. AudioKit.Settings.VoiceVolume.Bind(volume => { VoicePlayer.SetVolume(volume); })
  52. .DisposeWhenGameObjectDestroyed(this);
  53. AudioKit.Settings.IsMusicOn.Bind(musicOn =>
  54. {
  55. if (musicOn)
  56. {
  57. if (!string.IsNullOrEmpty(CurrentMusicName))
  58. {
  59. AudioKit.PlayMusic(CurrentMusicName);
  60. }
  61. }
  62. else
  63. {
  64. MusicPlayer.Stop();
  65. }
  66. }).DisposeWhenGameObjectDestroyed(this);
  67. AudioKit.Settings.IsVoiceOn.Bind(voiceOn =>
  68. {
  69. if (voiceOn)
  70. {
  71. if (!string.IsNullOrEmpty(CurrentVoiceName))
  72. {
  73. AudioKit.PlayVoice(CurrentVoiceName);
  74. }
  75. }
  76. else
  77. {
  78. VoicePlayer.Stop();
  79. }
  80. }).DisposeWhenGameObjectDestroyed(this);
  81. AudioKit.Settings.IsSoundOn.Bind(soundOn =>
  82. {
  83. if (soundOn)
  84. {
  85. }
  86. else
  87. {
  88. ForEachAllSound(player => player.Stop());
  89. }
  90. }).DisposeWhenGameObjectDestroyed(this);
  91. AudioKit.Settings.SoundVolume.Bind(soundVolume =>
  92. {
  93. ForEachAllSound(player => player.SetVolume(soundVolume));
  94. }).DisposeWhenGameObjectDestroyed(this);
  95. }
  96. private static Dictionary<string, List<AudioPlayer>> mSoundPlayerInPlaying =
  97. new Dictionary<string, List<AudioPlayer>>(30);
  98. public void ForEachAllSound(Action<AudioPlayer> operation)
  99. {
  100. foreach (var audioPlayer in mSoundPlayerInPlaying.SelectMany(keyValuePair => keyValuePair.Value))
  101. {
  102. operation(audioPlayer);
  103. }
  104. }
  105. public void AddSoundPlayer2Pool(AudioPlayer audioPlayer)
  106. {
  107. if (mSoundPlayerInPlaying.ContainsKey(audioPlayer.Name))
  108. {
  109. mSoundPlayerInPlaying[audioPlayer.Name].Add(audioPlayer);
  110. }
  111. else
  112. {
  113. mSoundPlayerInPlaying.Add(audioPlayer.Name, new List<AudioPlayer> {audioPlayer});
  114. }
  115. }
  116. public void RemoveSoundPlayerFromPool(AudioPlayer audioPlayer)
  117. {
  118. mSoundPlayerInPlaying[audioPlayer.Name].Remove(audioPlayer);
  119. }
  120. #region 对外接口
  121. public void Init()
  122. {
  123. Debug.Log("AudioManager.Init");
  124. }
  125. private AudioListener mAudioListener;
  126. public void CheckAudioListener()
  127. {
  128. // 确保有一个AudioListener
  129. if (!mAudioListener)
  130. {
  131. mAudioListener = FindObjectOfType<AudioListener>();
  132. }
  133. if (!mAudioListener)
  134. {
  135. mAudioListener = gameObject.AddComponent<AudioListener>();
  136. }
  137. }
  138. public string CurrentMusicName { get; set; }
  139. public string CurrentVoiceName { get; set; }
  140. #endregion
  141. public static void PlayVoiceOnce(string voiceName)
  142. {
  143. if (string.IsNullOrEmpty(voiceName))
  144. {
  145. return;
  146. }
  147. var unit = SafeObjectPool<AudioPlayer>.Instance.Allocate();
  148. unit.SetAudio(Instance.gameObject, voiceName, false);
  149. }
  150. #region 单例实现
  151. public static AudioManager Instance
  152. {
  153. get { return MonoSingletonProperty<AudioManager>.Instance; }
  154. }
  155. #endregion
  156. public void ClearAllPlayingSound()
  157. {
  158. mSoundPlayerInPlaying.Clear();
  159. }
  160. }
  161. }