audioManager.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import { Lodash } from './lodash';
  2. import { _decorator, assetManager, AudioClip } from "cc";
  3. import { StorageManager } from "./storageManager";
  4. import { ResourceUtil } from "./resourceUtil";
  5. import { gameManager } from '../gameManager';
  6. const { ccclass, property } = _decorator;
  7. @ccclass("AudioManager")
  8. export class AudioManager {
  9. public dictWeaponSoundIndex: any = {};
  10. public musicVolume: number = 0.8;
  11. public soundVolume: number = 1;
  12. public audios: any = {};
  13. public arrSound: any = [];
  14. public static _instance: AudioManager;
  15. public static get instance () {
  16. if (this._instance) {
  17. return this._instance;
  18. }
  19. this._instance = new AudioManager();
  20. this._instance.init();
  21. return this._instance;
  22. }
  23. public init () {
  24. this.musicVolume = this.getAudioSetting(true) ? 0.8: 0;
  25. this.soundVolume = this.getAudioSetting(false) ? 1 : 0;
  26. }
  27. public onAppShow () {
  28. for (let name in this.audios) {
  29. let audio = this.audios[name];
  30. if (audio.loop) {
  31. //属于无限循环的,则需要在wx环境下自己开启播放
  32. audio.clip.play();
  33. }
  34. }
  35. }
  36. public getAudioSetting (isMusic: boolean) {
  37. let state;
  38. if (isMusic) {
  39. state = StorageManager.instance.getGlobalData('music');
  40. } else {
  41. state = StorageManager.instance.getGlobalData('sound');
  42. }
  43. // console.log('Config for [' + (isMusic ? 'Music' : 'Sound') + '] is ' + state);
  44. return !state || state === 'true' ? true : false;
  45. }
  46. /**
  47. * 播放音乐
  48. * @param {String} name 音乐名称可通过constants.AUDIO_MUSIC 获取
  49. * @param {Boolean} loop 是否循环播放
  50. */
  51. public playMusic (name:string, loop: boolean) {
  52. // let path = 'audio/music/' + name;
  53. let path = ''+ name;
  54. //微信特殊处理,除一开场的音乐,其余的放在子包里头
  55. // if (name !== 'click') {
  56. // path = path; //微信特殊处理,除一开场的音乐,其余的放在子包里头
  57. // }
  58. // ResourceUtil.loadRes(path, AudioClip, (err: any, clip: any)=> {
  59. // let tmp = {} as any;
  60. // tmp.clip = clip;
  61. // tmp.loop = loop;
  62. // tmp.isMusic = true;
  63. // this.audios[name] = tmp;
  64. // this.playClip(name, true);
  65. // });
  66. let call_back = (err: any, clip: AudioClip)=> {
  67. let tmp = {} as any;
  68. tmp.clip = clip;
  69. tmp.loop = loop;
  70. tmp.isMusic = true;
  71. this.audios[name] = tmp;
  72. this.playClip(name, true);
  73. }
  74. if(gameManager.mp3_cache.get(path)){
  75. let clip = gameManager.mp3_cache.get(path);
  76. call_back(null,clip)
  77. }else{
  78. assetManager.loadRemote(name, call_back );
  79. }
  80. }
  81. /**
  82. * 播放音效
  83. * @param {String} name 音效名称可通过constants.AUDIO_SOUND 获取
  84. * @param {Boolean} loop 是否循环播放
  85. */
  86. public playSound (name:string, loop:boolean = false) {
  87. if (!this.soundVolume) {
  88. return;
  89. }
  90. //音效一般是多个的,不会只有一个
  91. // let path = 'audio/sound/';
  92. let path = '';
  93. // if (name !== 'click') {
  94. // path = path; //微信特殊处理,除一开场的音乐,其余的放在子包里头
  95. // }
  96. // ResourceUtil.loadRes(path + name, AudioClip, (err: any, clip: any)=> {
  97. // let tmp = {} as any;
  98. // tmp.clip = clip;
  99. // tmp.loop = loop;
  100. // tmp.isMusic = false;
  101. // this.arrSound.push(tmp);
  102. // if (loop) {
  103. // this.audios[name] = tmp;
  104. // clip.setLoop(loop);
  105. // }
  106. // clip.setVolume(this.soundVolume);
  107. // clip.playOneShot();
  108. // clip.once('ended', ()=>{
  109. // Lodash.remove(this.arrSound, (obj: any)=>{
  110. // return obj.clip === tmp.clip;
  111. // });
  112. // })
  113. // });
  114. let call_back = (err: any, clip: any)=> {
  115. let tmp = {} as any;
  116. tmp.clip = clip;
  117. tmp.loop = loop;
  118. tmp.isMusic = false;
  119. this.arrSound.push(tmp);
  120. if (loop) {
  121. this.audios[name] = tmp;
  122. clip.setLoop(loop);
  123. }
  124. clip.setVolume(this.soundVolume);
  125. clip.playOneShot();
  126. clip.once('ended', ()=>{
  127. Lodash.remove(this.arrSound, (obj: any)=>{
  128. return obj.clip === tmp.clip;
  129. });
  130. })
  131. }
  132. if(gameManager.mp3_cache.get(path)){
  133. let clip = gameManager.mp3_cache.get(path);
  134. call_back(null,clip)
  135. }else{
  136. assetManager.loadRemote(name, call_back );
  137. }
  138. }
  139. public playClip (name: string, isMusic?: boolean) {
  140. // console.log('playClip: ' + JSON.stringify(this.audios));
  141. this.stopAll()
  142. let audio = this.audios[name];
  143. // if (typeof audio.audioId === "number") {
  144. // let state = cc.audioEngine.getState(audio.audioId);
  145. // if (state === cc.audioEngine.AudioState.PLAYING && audio.loop) return;
  146. // }
  147. let volume = this.musicVolume;
  148. if (!isMusic) {
  149. volume = this.soundVolume;
  150. }
  151. let clip = audio.clip as AudioClip;
  152. clip.setVolume(volume);
  153. clip.setLoop(audio.loop);
  154. clip.play();
  155. // let audioId = cc.audioEngine.play(audio.clip, audio.loop, volume);
  156. // audio.audioId = audioId;
  157. }
  158. public stop (name: any) {
  159. if (this.audios.hasOwnProperty(name)) {
  160. let audio = this.audios[name];
  161. audio.clip.stop();
  162. }
  163. }
  164. public stopAll () {
  165. for (const i in this.audios) {
  166. if (this.audios.hasOwnProperty(i)) {
  167. let audio = this.audios[i];
  168. audio.clip.stop();
  169. }
  170. }
  171. }
  172. public setMusic (flag: any) {
  173. if (typeof flag !== "number") {
  174. flag = flag ? 1 : 0;
  175. }
  176. this.musicVolume = flag;
  177. for (let item in this.audios) {
  178. if (this.audios.hasOwnProperty(item) && this.audios[item].isMusic) {
  179. // this.changeState(item, flag);
  180. let audio = this.audios[item];
  181. audio.clip.setVolume(this.musicVolume);
  182. }
  183. }
  184. }
  185. //看广告时先将音乐暂停
  186. public pauseAll () {
  187. console.log("pause all music!!!");
  188. for (let item in this.audios) {
  189. if (this.audios.hasOwnProperty(item)) {
  190. let audio = this.audios[item];
  191. audio.clip.pause();
  192. }
  193. }
  194. }
  195. public resumeAll () {
  196. for (let item in this.audios) {
  197. if (this.audios.hasOwnProperty(item)) {
  198. let audio = this.audios[item];
  199. audio.clip.play();
  200. }
  201. }
  202. }
  203. public openMusic () {
  204. this.setMusic(0.8);
  205. StorageManager.instance.setGlobalData('music', 'true');
  206. }
  207. public closeMusic () {
  208. this.setMusic(0);
  209. StorageManager.instance.setGlobalData('music', 'false');
  210. }
  211. public openSound () {
  212. this.setSound(1);
  213. StorageManager.instance.setGlobalData('sound', 'true');
  214. }
  215. public closeSound () {
  216. this.setSound(0);
  217. StorageManager.instance.setGlobalData('sound', 'false');
  218. }
  219. public setSound (flag: any) {
  220. this.soundVolume = flag;
  221. for (let item in this.audios) {
  222. if (this.audios.hasOwnProperty(item) && !this.audios[item].isMusic) {
  223. // this.changeState(item, flag);
  224. let audio = this.audios[item];
  225. audio.clip.setVolume(this.soundVolume);
  226. }
  227. }
  228. for (let idx = 0; idx < this.arrSound.length; idx++) {
  229. let audio = this.arrSound[idx];
  230. audio.clip.setVolume(this.soundVolume);
  231. }
  232. }
  233. public stopSingleSound (name: string) {
  234. if (this.audios.hasOwnProperty(name) && !this.audios[name].isMusic) {
  235. let audio = this.audios[name];
  236. audio.clip.stop();
  237. }
  238. }
  239. }