AudioMgr.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import PlatUtils from "../../common-plugin/Scripts/PlatUtils";
  2. import { cocosz } from "./CocosZ";
  3. export default class AudioMgr {
  4. private static _inst: AudioMgr;
  5. public static get inst(): AudioMgr {
  6. if (!AudioMgr._inst) {
  7. AudioMgr._inst = new AudioMgr();
  8. }
  9. return AudioMgr._inst;
  10. }
  11. private _curMusicId: number = -1;
  12. private _curMusicClip: cc.AudioClip = null;
  13. private _videoOn: boolean = false;
  14. public set videoOn(value: boolean) {
  15. this._videoOn = value;
  16. }
  17. public get videoOn() {
  18. return this._videoOn;
  19. }
  20. public get AudioOn() {
  21. return cocosz.dataMgr.AudioOn;
  22. }
  23. public set AudioOn(value: boolean) {
  24. cocosz.dataMgr.AudioOn = value;
  25. if (value) {
  26. if (this._curMusicClip) {
  27. this._curMusicId = this._play(this._curMusicClip, true, 1);
  28. }
  29. } else {
  30. this.stopAll();
  31. }
  32. }
  33. // audioList: Array<cc.AudioClip> = [];
  34. /**
  35. * 播放背景音乐
  36. * @param id
  37. */
  38. public playBgm(loop: boolean = true) {
  39. if (!this.AudioOn) return;
  40. let str = "bgm_ui";
  41. if (cc.director.getScene().name == "Home") {
  42. str = "bgm_ui";
  43. } else if (cc.director.getScene().name == "Game") {
  44. str = "bgm_6";
  45. } else {
  46. return;
  47. }
  48. const res_bgm = cocosz.resMgr.getRes(str, cc.AudioClip);
  49. if (res_bgm) {
  50. this.stopAll();
  51. setTimeout(() => { cc.audioEngine.playMusic(res_bgm, true); }, 0);
  52. }
  53. }
  54. _guideAudioId: number = -1;
  55. /** 播放新手音效 */
  56. public playGuideAudio(name: string) {
  57. if (this._guideAudioId != -1) {
  58. cc.audioEngine.stop(this._guideAudioId);
  59. this._guideAudioId = -1;
  60. }
  61. this._guideAudioId = cocosz.audioMgr.playEffect(name);
  62. }
  63. /**
  64. * 播放音效
  65. * @param url 音频路径
  66. * @param loop 是否循环
  67. * @param volume 音量
  68. */
  69. public playEffect(url: string, loop: boolean = false, volume: number = 1) {
  70. if (this.videoOn || !this.AudioOn) return;
  71. const music = cocosz.resMgr.getRes(url, cc.AudioClip);
  72. if (music) {
  73. this.audioIdList[url] = cc.audioEngine.play(music, loop, volume);
  74. return this.audioIdList[url];
  75. }
  76. }
  77. /**
  78. * 播放音效
  79. * @param url 音频路径
  80. * @param loop 是否循环
  81. * @param volume 音量
  82. */
  83. public playClip(clip: cc.AudioClip, loop: boolean = false, volume: number = 1) {
  84. if (this.videoOn || !this.AudioOn) return;
  85. if (clip && clip.isValid) {
  86. this.audioIdList[clip.name] = cc.audioEngine.play(clip, loop, volume);
  87. return this.audioIdList[clip.name];
  88. }
  89. }
  90. audioIdList: Array<number> = [];
  91. public stopEffect(url: string) {
  92. if (this.audioIdList[url] >= 0) {
  93. cc.audioEngine.stop(this.audioIdList[url]);
  94. this.audioIdList[url] = null;
  95. }
  96. }
  97. public checkEffect(url: string) {
  98. if (this.audioIdList[url]) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. /**按钮音效 */
  104. public playBtnEffect() {
  105. return new Promise((resolve, reject) => {
  106. this.playEffect("btn");
  107. setTimeout(() => {
  108. resolve(1);
  109. }, 200);
  110. });
  111. }
  112. /**
  113. * 播放胜利音效
  114. */
  115. public playEffectWinner(volume: number = 1) {
  116. this.stopAll();
  117. setTimeout(() => {
  118. if (cocosz.gameMode != 6) {
  119. this.playEffect("win");
  120. } else {
  121. this.playEffect("zombie_win");
  122. }
  123. })
  124. }
  125. /**
  126. * 播放失败音效
  127. */
  128. public playEffectFailed(volume: number = 1) {
  129. this.stopAll();
  130. setTimeout(() => {
  131. if (cocosz.gameMode != 6) {
  132. this.playEffect("fail");
  133. } else {
  134. this.playEffect("zombie_fail");
  135. }
  136. })
  137. }
  138. /**
  139. * 暂停所有音效
  140. */
  141. public pauseAll() {
  142. cc.audioEngine.pauseAll();
  143. }
  144. public resumeAll() {
  145. // utils.delayCall(() => {
  146. if (!this._videoOn) cc.audioEngine.resumeAll();
  147. // }, 0)
  148. }
  149. public stopAll() {
  150. cc.audioEngine.stopAll();
  151. this.audioIdList = [];
  152. }
  153. private _play(clip: cc.AudioClip, loop: boolean, volume: number) {
  154. if (!this.AudioOn) return -1;
  155. return cc.audioEngine.play(clip, loop, volume);
  156. }
  157. }