AudioManager.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import BasicManager from "../Basic/BasicManager";
  2. import { _IPlaySound } from "./Data/AudioConfig";
  3. import { _EJsonRes, _IAudioConfig } from "../Data/CommonDataType";
  4. import EventManager from "../Event/EventManager";
  5. import { EventName } from "../Event/EventName";
  6. import JsonResources from "../Resources/JsonResources";
  7. import TimeManager from "../Time/TimeManager";
  8. import Tool from "../Tool/Tool";
  9. /**
  10. * 音频管理器 - 逻辑层
  11. */
  12. export default class AudioManager extends BasicManager {
  13. /**当前背景音乐 */
  14. private bgm: string;
  15. /**音频ID列表 */
  16. private audioIDs: Map<string, cc.AudioSource>;
  17. /**当前播放音频ID列表 */
  18. private currentAudioIDs: Map<string, cc.AudioSource>;
  19. /**资源表 */
  20. private res: Array<_IAudioConfig>;
  21. constructor () {
  22. super();
  23. Tool.log("生成音频管理器");
  24. this.audioIDs = new Map<string, cc.AudioSource>();
  25. this.currentAudioIDs = new Map<string, cc.AudioSource>();
  26. //失去舞台焦点(切出游戏)的处理
  27. EventManager.onEvent(EventName.Phone.Phone_Hide, this, () => {
  28. cc.audioEngine.pauseAll();
  29. });
  30. //获得舞台焦点(切回游戏)的处理
  31. EventManager.onEvent(EventName.Phone.Phone_Show, this, () => {
  32. cc.audioEngine.resumeAll();
  33. });
  34. EventManager.onEvent(EventName.Audio.Audio_PauseMusic, this, () => {
  35. cc.audioEngine.pauseMusic();
  36. });
  37. EventManager.onEvent(EventName.Audio.Audio_ResumeMusic, this, () => {
  38. cc.audioEngine.resumeMusic();
  39. });
  40. EventManager.onEvent(EventName.Audio.Audio_PauseSound, this, (url: string) => {
  41. if (url != null) {
  42. this.audioIDs.get(url) && this.audioIDs.get(url).pause();
  43. }
  44. else {
  45. this.currentAudioIDs.forEach((value: cc.AudioSource, key: string) => {
  46. value.pause();
  47. });
  48. }
  49. });
  50. EventManager.onEvent(EventName.Audio.Audio_ResumeSound, this, (url: string) => {
  51. if (url != null) {
  52. let value: cc.AudioSource = this.audioIDs.get(url);
  53. if (value != null) {
  54. if (value.getCurrentTime() == 0) {
  55. value.play();
  56. }
  57. else {
  58. value.resume();
  59. }
  60. }
  61. }
  62. else {
  63. this.currentAudioIDs.forEach((value: cc.AudioSource, key: string) => {
  64. if (value.getCurrentTime() == 0) {
  65. value.play();
  66. }
  67. else {
  68. value.resume();
  69. }
  70. });
  71. }
  72. });
  73. EventManager.onEvent(EventName.Audio.Audio_PlayMusic, this, this.onPlayMusic);
  74. EventManager.onEvent(EventName.Audio.Audio_StopMusic, this, this.onStopMusic);
  75. EventManager.onEvent(EventName.Audio.Audio_PlaySound, this, this.onPlaySound);
  76. EventManager.onEvent(EventName.Audio.Audio_StopSound, this, this.onStopSound);
  77. }
  78. /**播放背景音乐 */
  79. public onPlayMusic (url: string): void {
  80. if (this.res == null) {
  81. this.res = JsonResources.getResources(_EJsonRes.Audio);
  82. }
  83. if (this.bgm == url) {
  84. if (this.audioIDs.has(url) == true) {
  85. this.audioIDs.get(url).play();
  86. }
  87. return;
  88. }
  89. this.bgm = url;
  90. cc.assetManager.loadBundle("res/audio", (err: Error, prefab: cc.AssetManager.Bundle) => {
  91. prefab.load(url, cc.AudioClip, (error: Error, clip: cc.AudioClip) => {
  92. if (this.audioIDs == null || clip == null) {
  93. return;
  94. }
  95. let audioSource: cc.AudioSource;
  96. if (this.audioIDs.has(url) == true) {
  97. audioSource = this.audioIDs.get(url);
  98. }
  99. else {
  100. audioSource = new cc.AudioSource();
  101. audioSource.loop = (this.res[url] != null)? this.res[url].loop:true;
  102. audioSource.clip = prefab.get(url);
  103. audioSource.volume = (this.res[url] != null)? this.res[url].volume:1;
  104. this.audioIDs.set(url, audioSource);
  105. }
  106. audioSource.play();
  107. });
  108. });
  109. }
  110. /**停止背景音乐 */
  111. private onStopMusic (url: string): void {
  112. if (this.audioIDs.has(url) == true) {
  113. this.audioIDs.get(url).stop();
  114. }
  115. }
  116. /**播放音效 */
  117. public onPlaySound (playSound: _IPlaySound): void {
  118. if (this.res == null) {
  119. this.res = JsonResources.getResources(_EJsonRes.Audio);
  120. }
  121. if (this.res == null) {
  122. return;
  123. }
  124. cc.assetManager.loadBundle("res/audio", (err: Error, prefab: cc.AssetManager.Bundle) => {
  125. prefab.load(playSound.url, cc.AudioClip, (error: Error, clip: cc.AudioClip) => {
  126. if (this.audioIDs == null || clip == null) {
  127. return;
  128. }
  129. let audioSource: cc.AudioSource;
  130. if (this.audioIDs.has(playSound.url) == true) {
  131. audioSource = this.audioIDs.get(playSound.url);
  132. }
  133. else {
  134. audioSource = cc.director.getScene().getChildByName('Canvas').addComponent(cc.AudioSource);
  135. audioSource.loop = (this.res[playSound.url] != null)? this.res[playSound.url].loop:false;
  136. audioSource.clip = prefab.get(playSound.url);
  137. audioSource.volume = (this.res[playSound.url] != null)? this.res[playSound.url].volume:1;
  138. this.audioIDs.set(playSound.url, audioSource);
  139. this.currentAudioIDs.set(playSound.url, audioSource);
  140. }
  141. audioSource.play();
  142. TimeManager.scheduleOnce(this, () => {
  143. this.currentAudioIDs.delete(playSound.url);
  144. playSound.callback && playSound.callback();
  145. }, clip.duration);
  146. });
  147. });
  148. }
  149. /**停止音效 */
  150. public onStopSound (url: string): void {
  151. if (this.audioIDs.has(url) == true) {
  152. this.currentAudioIDs.delete(url);
  153. this.audioIDs.get(url).stop();
  154. }
  155. }
  156. _destroy () {
  157. Tool.log("销毁音频管理器");
  158. cc.audioEngine.uncacheAll();
  159. this.audioIDs = null;
  160. this.res = null;
  161. super._destroy();
  162. }
  163. }