SoundMgr.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { SaveUtil } from "../help/SaveUtil";
  2. import ResMgr from "./ResMgr";
  3. const { ccclass, property } = cc._decorator;
  4. var MUSIC_NUM = 2;
  5. var EFFECT_NUM = 8;
  6. @ccclass
  7. export default class SoundMgr extends cc.Component {
  8. public static Instance: SoundMgr = null;
  9. private effect_volume: number = 1;
  10. private music_volume: number = 0.9;
  11. private music_as: Array<cc.AudioSource> = [];
  12. private effect_as: Array<cc.AudioSource> = [];
  13. private curEff_as: number = 0;
  14. onLoad() {
  15. if (SoundMgr.Instance === null) {
  16. SoundMgr.Instance = this;
  17. }
  18. else {
  19. this.destroy();
  20. return;
  21. }
  22. // this.music_as = this.node.addComponent(cc.AudioSource);
  23. // this.music_as.volume = this.music_volume;
  24. this.MusicMute = SaveUtil.get("music_mute", "0") == "1";//默认不禁用
  25. // if (this.MusicMute) {
  26. // this.music_as.volume = 0;
  27. // }
  28. this.EffMute = SaveUtil.get("effect_mute", "0") == "1";//默认不禁用
  29. // for(var i = 0; i < EFFECT_NUM; i ++) {
  30. // var as = this.node.addComponent(cc.AudioSource);
  31. // this.effect_as.push(as);
  32. // as.volume = this.effect_volume;
  33. // if (this.EffMute) {
  34. // as.volume = 0;
  35. // }
  36. // }
  37. // this.music_volume = 0.05;
  38. this.initAudio(EFFECT_NUM,this.effect_as,this.effect_volume,this.EffMute);
  39. this.initAudio(MUSIC_NUM,this.music_as,this.music_volume,this.MusicMute);
  40. this.curEff_as = 0;
  41. }
  42. private initAudio(num:number,arr:Array<cc.AudioSource>,volume:number,mute:boolean ){
  43. for(var i = 0; i < num; i ++) {
  44. var as = this.node.addComponent(cc.AudioSource);
  45. arr.push(as);
  46. as.volume = volume;
  47. if (mute) {
  48. as.volume = 0;
  49. }
  50. }
  51. }
  52. get_music_volume() {
  53. return this.music_volume;
  54. }
  55. set_music_volume(value) {
  56. for(var i = 0; i < this.music_as.length; i ++) {
  57. this.music_as[i].volume = value;
  58. }
  59. this.music_volume = value;
  60. cc.sys.localStorage.setItem("music_volume", value);
  61. }
  62. private musicMute:boolean = true;
  63. get MusicMute():boolean{
  64. return this.musicMute;
  65. }
  66. set MusicMute(b_mute:boolean) {
  67. if (this.musicMute == b_mute) {
  68. return;
  69. }
  70. this.musicMute = b_mute;
  71. for(var i = 0; i < this.music_as.length; i ++) {
  72. if (b_mute) {
  73. this.music_as[i].volume = 0;
  74. }
  75. else {
  76. this.music_as[i].volume = this.music_volume;
  77. }
  78. }
  79. cc.sys.localStorage.setItem("music_mute", b_mute?"1":"0");
  80. }
  81. ToogleMusicMute(){
  82. this.MusicMute = !this.MusicMute;
  83. return this.MusicMute;
  84. }
  85. get_effect_volume() {
  86. return this.effect_volume;
  87. }
  88. set_effect_volume(value) {
  89. for(var i = 0; i < this.effect_as.length; i ++) {
  90. this.effect_as[i].volume = value;
  91. }
  92. this.effect_volume = value;
  93. cc.sys.localStorage.setItem("effect_volume", value);
  94. }
  95. private effMute = false;
  96. get EffMute():boolean{
  97. return this.effMute;
  98. }
  99. set EffMute(b_mute:boolean) {
  100. if (this.EffMute == b_mute) {
  101. return;
  102. }
  103. this.effMute = b_mute;
  104. for(var i = 0; i < this.effect_as.length; i ++) {
  105. if (b_mute) {
  106. this.effect_as[i].volume = 0;
  107. }
  108. else {
  109. this.effect_as[i].volume = this.effect_volume;
  110. }
  111. }
  112. cc.sys.localStorage.setItem("effect_mute", b_mute?"1":"0");
  113. }
  114. ToogleEffectMute(){
  115. this.EffMute = !this.EffMute;
  116. return this.EffMute;
  117. }
  118. play_main_music(url, loop) {
  119. loop = (loop) ? true : false;
  120. var as = this.music_as[0];
  121. as.loop = loop;
  122. as.clip = ResMgr.Instance.getAsset("Sounds", url);
  123. if (as.clip) {
  124. as.play();
  125. }
  126. else {
  127. cc.error("music audio clip null: ", url);
  128. }
  129. }
  130. play_sub_music(url, loop) {
  131. loop = (loop) ? true : false;
  132. var as = this.music_as[1];
  133. as.loop = loop;
  134. as.clip = ResMgr.Instance.getAsset("Sounds", url);
  135. if (as.clip) {
  136. as.play();
  137. }
  138. else {
  139. cc.error("music audio clip null: ", url);
  140. }
  141. }
  142. stop_sub_music(){
  143. this.music_as[1].stop();
  144. }
  145. stop_music() {
  146. // this.music_as.stop();
  147. for(var i = 0; i < this.music_as.length; i ++) {
  148. this.music_as[i].stop();
  149. }
  150. }
  151. play_effect(url) {
  152. var as = this.effect_as[this.curEff_as];
  153. this.curEff_as ++;
  154. if (this.curEff_as >= EFFECT_NUM) {
  155. this.curEff_as = 0;
  156. }
  157. as.clip = ResMgr.Instance.getAsset("Sounds", url);
  158. if (as.clip) {
  159. as.play();
  160. }
  161. else {
  162. cc.error("effect audio clip null: ", url);
  163. }
  164. }
  165. playClickBtn(){
  166. this.play_effect("BMButton_click");
  167. }
  168. /**
  169. * 获取音乐开关状态
  170. */
  171. getSwitchMusic(){
  172. return this.EffMute;
  173. }
  174. /**
  175. * 暂停当前播放音乐
  176. */
  177. setPauseMusic(){
  178. cc.audioEngine.pauseMusic();
  179. }
  180. /**
  181. * 恢复当前被暂停音乐音乐
  182. */
  183. setResumeMusic(){
  184. cc.audioEngine.resumeMusic();
  185. }
  186. /**
  187. * 重新播放该背景音乐
  188. */
  189. replayMusic(){
  190. cc.audioEngine.resumeMusic();
  191. }
  192. /**
  193. * 暂停播放音乐
  194. * releaseData: 控制是否释放音乐资源 true释放资源 | false不释放资源
  195. */
  196. setStopMusic(releaseData = true){
  197. cc.audioEngine.stopMusic();//releaseData
  198. }
  199. }