AudioMng.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. const GameModule = require("../utils/GameModule");
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. bgm: {
  6. tooltip: '背景音乐',
  7. default: null,
  8. type: cc.AudioClip
  9. },
  10. buttonClickAudio: {
  11. tooltip: '普通按钮点击音效',
  12. default: null,
  13. type: cc.AudioClip
  14. },
  15. getAwardAudio: {
  16. tooltip: '获得技能、奖励音效',
  17. default: null,
  18. type: cc.AudioClip
  19. },
  20. updateBuildingAudio: {
  21. tooltip: '升级或解锁建筑,购买明星音效',
  22. default: null,
  23. type: cc.AudioClip
  24. },
  25. clickCatAudio: {
  26. tooltip: '点击招财猫音效',
  27. default: null,
  28. type: cc.AudioClip
  29. },
  30. getDrawAudio: {
  31. tooltip: '抽奖音效',
  32. default: null,
  33. type: cc.AudioClip
  34. },
  35. getStarAudio: {
  36. tooltip: '获得明星音效',
  37. default: null,
  38. type: cc.AudioClip
  39. },
  40. volume: {
  41. tooltip: '总音量',
  42. default: 1,
  43. type: cc.Integer
  44. },
  45. bgmOpen: {
  46. get: function() {
  47. return this._bgmOpen;
  48. },
  49. set: function(value) {
  50. this._bgmOpen = value;
  51. if (this._bgmOpen) {
  52. this.playBgm();
  53. } else {
  54. this.stopBgm();
  55. }
  56. cc.sys.localStorage.setItem("game_bgm_state", value);
  57. }
  58. },
  59. soundEffectOpen: {
  60. get: function() {
  61. return this._soundEffectOpen;
  62. },
  63. set: function(value) {
  64. this._soundEffectOpen = value;
  65. cc.sys.localStorage.setItem("game_sound_effect_state", value);
  66. }
  67. }
  68. },
  69. onLoad () {
  70. GameModule.audioMng = this;
  71. this.currentAudio = {};
  72. this.stopAll();
  73. let bgmState = cc.sys.localStorage.getItem("game_bgm_state");
  74. if (bgmState!= undefined) {
  75. if (typeof bgmState == "string" && bgmState == "") {
  76. this.bgmOpen = true;
  77. } else {
  78. this.bgmOpen = bgmState;
  79. }
  80. } else {
  81. this.bgmOpen = true;
  82. }
  83. let soundEffectState = cc.sys.localStorage.getItem("game_sound_effect_state");
  84. if (soundEffectState!= undefined) {
  85. if (typeof soundEffectState == "string" && soundEffectState == "") {
  86. this.soundEffectOpen = true;
  87. } else {
  88. this.soundEffectOpen = soundEffectState;
  89. }
  90. } else {
  91. this.soundEffectOpen = true;
  92. }
  93. this.playBgm();
  94. },
  95. /**
  96. * 暂停所有音效
  97. */
  98. stopAll () {
  99. cc.audioEngine.stopAll();
  100. delete this.currentAudio['bgm'];
  101. },
  102. /**
  103. * 播放背景音乐
  104. */
  105. playBgm () {
  106. if (this.currentAudio.hasOwnProperty('bgm')) {
  107. return;
  108. }
  109. if (!this.bgmOpen) { return }
  110. this.currentAudio['bgm'] = cc.audioEngine.play(this.bgm, true, this.volume);
  111. },
  112. /**
  113. * 停止播放背景音乐
  114. */
  115. pauseBgm () {
  116. cc.audioEngine.pause(this.currentAudio['bgm']);
  117. },
  118. /**
  119. * 暂停播放背景音乐
  120. */
  121. stopBgm () {
  122. cc.audioEngine.stop(this.currentAudio['bgm']);
  123. delete this.currentAudio['bgm'];
  124. },
  125. /**
  126. * 播放按钮音效
  127. */
  128. playClickButton () {
  129. if (!this.soundEffectOpen) { return }
  130. if (window.tt != undefined) {
  131. // if (this.playClickButtonAudioContext != null && this.playClickButtonAudioContext != undefined) {
  132. // // this.playClickButtonAudioContext.destory();
  133. // }
  134. this.playClickButtonAudioContext = tt.createInnerAudioContext();
  135. this.playClickButtonAudioContext.autoplay = false;
  136. this.playClickButtonAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/buttonClick.mp3';
  137. this.playClickButtonAudioContext.play();
  138. // this.playClickButtonAudioContext.onEnded(() => {
  139. // this.playClickButtonAudioContext.destory();
  140. // this.playClickButtonAudioContext = null;
  141. // })
  142. } else {
  143. cc.audioEngine.play(this.buttonClickAudio, false, this.volume);
  144. }
  145. },
  146. /**
  147. * 播放获得奖励音效
  148. */
  149. playGetAward () {
  150. if (!this.soundEffectOpen) { return }
  151. if (window.tt != undefined) {
  152. // if (this.playGetAwardAudioContext != null && this.playGetAwardAudioContext != undefined) {
  153. // // this.playGetAwardAudioContext.destory();
  154. // }
  155. this.playGetAwardAudioContext = tt.createInnerAudioContext();
  156. this.playGetAwardAudioContext.autoplay = false;
  157. this.playGetAwardAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/getAward.mp3';
  158. this.playGetAwardAudioContext.play();
  159. // this.playGetAwardAudioContext.onEnded(() => {
  160. // this.playGetAwardAudioContext.destory();
  161. // this.playGetAwardAudioContext = null;
  162. // })
  163. } else {
  164. cc.audioEngine.play(this.getAwardAudio, false, this.volume);
  165. }
  166. },
  167. /**
  168. * 播放升级建筑音效
  169. */
  170. playUpdateBuilding () {
  171. if (!this.soundEffectOpen) { return }
  172. if (window.tt != undefined) {
  173. // if (this.updateBuildingAudioContext != undefined && this.updateBuildingAudioContext != null) {
  174. // // this.updateBuildingAudioContext.destory();
  175. // }
  176. this.updateBuildingAudioContext = tt.createInnerAudioContext();
  177. this.updateBuildingAudioContext.autoplay = false;
  178. this.updateBuildingAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/updateBuilding.mp3';
  179. this.updateBuildingAudioContext.play();
  180. // this.updateBuildingAudioContext.onEnded(() => {
  181. // this.updateBuildingAudioContext.destory();
  182. // this.updateBuildingAudioContext = null;
  183. // })
  184. } else {
  185. cc.audioEngine.play(this.updateBuildingAudio, false, this.volume);
  186. }
  187. },
  188. /**
  189. * 点击招财猫
  190. */
  191. playClickCat () {
  192. if (!this.soundEffectOpen) { return }
  193. if (window.tt != undefined) {
  194. // if (this.innerAudioContext != undefined && this.innerAudioContext != null) {
  195. // // this.innerAudioContext.destory();
  196. // }
  197. this.innerAudioContext = tt.createInnerAudioContext();
  198. this.innerAudioContext.autoplay = false;
  199. this.innerAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/clickCat.mp3';
  200. this.innerAudioContext.play();
  201. // this.innerAudioContext.onEnded(() => {
  202. // this.innerAudioContext.destory();
  203. // this.innerAudioContext = null;
  204. // })
  205. } else {
  206. cc.audioEngine.play(this.clickCatAudio, false, this.volume)
  207. }
  208. },
  209. /**
  210. * 抽奖音效
  211. */
  212. playGetDraw () {
  213. if (!this.soundEffectOpen) { return }
  214. if (window.tt != undefined) {
  215. // if (this.getDrawAudioContext != undefined && this.getDrawAudioContext != null) {
  216. // // this.getDrawAudioContext.destory();
  217. // }
  218. this.getDrawAudioContext = tt.createInnerAudioContext();
  219. this.getDrawAudioContext.autoplay = false;
  220. this.getDrawAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/getDraw.mp3';
  221. this.getDrawAudioContext.play();
  222. // this.getDrawAudioContext.onEnded(() => {
  223. // this.getDrawAudioContext.destory();
  224. // this.getDrawAudioContext = null;
  225. // })
  226. } else {
  227. cc.audioEngine.play(this.getDrawAudio, false, this.volume);
  228. }
  229. },
  230. /**
  231. * 获取到明星音效
  232. */
  233. playGetStar () {
  234. if (!this.soundEffectOpen) { return }
  235. if (window.tt != undefined) {
  236. // if (this.playGetStarAudioContext != undefined && this.playGetStarAudioContext != null) {
  237. // // this.playGetStarAudioContext.destory();
  238. // }
  239. this.playGetStarAudioContext = tt.createInnerAudioContext();
  240. this.playGetStarAudioContext.autoplay = false;
  241. this.playGetStarAudioContext.src = 'https://pub.dwstatic.com/wxgame/taptapstar_toutiao_test/sheet/getStar.mp3';
  242. this.playGetStarAudioContext.play();
  243. // this.playGetStarAudioContext.onEnded(() => {
  244. // this.playGetStarAudioContext.destory();
  245. // this.playGetStarAudioContext = null;
  246. // })
  247. } else {
  248. cc.audioEngine.play(this.getStarAudio, false, this.volume);
  249. }
  250. },
  251. /**
  252. * 停止播放获取明星音效
  253. */
  254. // pauseGetStar () {
  255. // this.currentAudio['bgm'] = cc.audioEngine.play(this.bgm, true, this.volume);
  256. // cc.audioEngine.pause(this.currentAudio['bgm']);
  257. // },
  258. // update (dt) {},
  259. });