AudioM.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // Learn TypeScript:
  2. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
  4. // Learn Attribute:
  5. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
  10. import { AUDIO_TYPE } from "./GameM";
  11. const { ccclass, property } = cc._decorator;
  12. @ccclass
  13. export default class AudioM extends cc.Component {
  14. ///单例
  15. private static instance: AudioM = null
  16. public static get Instance() {
  17. if (this.instance == null) {
  18. this.instance = new AudioM()
  19. }
  20. return this.instance
  21. }
  22. private _switchMusic = true; // 音乐开关
  23. private _switchEffect = true; // 音效开关
  24. private _effectVolume = 1; // 音效音量
  25. private _musicVolume = 1; // 音乐音量
  26. private _switchTempEffect = true; // 暂时音效开关
  27. // LIFE-CYCLE CALLBACKS:
  28. // onLoad () {}
  29. start() {
  30. }
  31. public init() {
  32. // 获取本地开关设置
  33. let effect = cc.sys.localStorage.getItem("switchEffect");
  34. if (effect) {
  35. if (effect == "true") {
  36. this._switchEffect = true;
  37. }
  38. else {
  39. this._switchEffect = false;
  40. }
  41. }
  42. let music = cc.sys.localStorage.getItem("switchMusic");
  43. if (music) {
  44. if (music == "true") {
  45. this._switchMusic = true;
  46. }
  47. else {
  48. this._switchMusic = false;
  49. }
  50. }
  51. if (this._switchMusic) {
  52. this.playMusic(AUDIO_TYPE.bg, true)
  53. }
  54. }
  55. loopEffectId = -1;
  56. public playLoopEffect(url) {
  57. if (this._switchEffect && this._switchTempEffect) {
  58. cc.loader.loadRes('sounds/' + url, (err, cip) => {
  59. if (err) {
  60. console.error(err);
  61. }
  62. this.loopEffectId = cc.audioEngine.playEffect(cip, true);//this.bgmVolume
  63. })
  64. }
  65. }
  66. public stopLoopEffect() {
  67. if (this.loopEffectId != -1)
  68. cc.audioEngine.stopEffect(this.loopEffectId);
  69. }
  70. /**
  71. * 播放音效文件
  72. * url: 音效文件相对地址
  73. * loop: 是否循环播放
  74. */
  75. public playEffect(url, loop = false) {
  76. if (this._switchEffect && this._switchTempEffect) {
  77. cc.loader.loadRes('sounds/' + url, (err, cip) => {
  78. if (err) {
  79. console.error(err);
  80. }
  81. this.sayId = cc.audioEngine.playEffect(cip, false);//this.bgmVolume
  82. })
  83. }
  84. }
  85. private mateId: number = -1;
  86. /**播放伙伴音效*/
  87. public playMateEffect(url, loop = false) {
  88. this.stopMateEffect();
  89. if (loop) {
  90. if (this._switchMusic) {
  91. cc.loader.loadRes('sounds/' + url, (err, cip) => {
  92. if (err) {
  93. console.error(err);
  94. }
  95. this.mateId = cc.audioEngine.playEffect(cip, loop);//this.bgmVolume
  96. })
  97. }
  98. } else {
  99. if (this._switchEffect && this._switchTempEffect) {
  100. cc.loader.loadRes('sounds/' + url, (err, cip) => {
  101. if (err) {
  102. console.error(err);
  103. }
  104. this.mateId = cc.audioEngine.playEffect(cip, loop);//this.bgmVolume
  105. })
  106. }
  107. }
  108. }
  109. public stopMateEffect() {
  110. if (this.mateId != -1) {
  111. cc.audioEngine.stopEffect(this.mateId);
  112. this.mateId = -1;
  113. }
  114. }
  115. public pauseMateEffect() {
  116. if (this.mateId != -1)
  117. cc.audioEngine.pauseEffect(this.mateId);
  118. }
  119. public resumeMateEffect() {
  120. if (this.mateId != -1)
  121. cc.audioEngine.resumeEffect(this.mateId);
  122. }
  123. /**判断当前伙伴音效播放状态*/
  124. public IsMatePlayingEffect() {
  125. return cc.audioEngine.getState(this.mateId) == cc.audioEngine.AudioState.PLAYING;
  126. }
  127. private sayId = -1
  128. public playSay(url, loop = false) {
  129. if (this.sayId != -1) {
  130. this.stopEffect(this.sayId)
  131. this.sayId = -1
  132. }
  133. this.playEffect(url, loop)
  134. }
  135. /** 停止播放音效 */
  136. public stopEffect(id) {
  137. cc.audioEngine.stopEffect(id);
  138. }
  139. /**
  140. * 转换音效开关
  141. */
  142. public switchEffectFunc() {
  143. this._switchEffect = !this._switchEffect;
  144. if (!this._switchEffect) {
  145. this.setStopAllEffect();
  146. }
  147. cc.sys.localStorage.setItem("switchEffect", this._switchEffect.toString());
  148. }
  149. /**
  150. * 获取音效开关状态
  151. */
  152. public getSwitchEffect() {
  153. return this._switchEffect;
  154. }
  155. /**
  156. * 设置音效声音大小
  157. * value: 0.0 - 1.0
  158. */
  159. public setEffectVolume(value) {
  160. this._effectVolume = value;
  161. cc.audioEngine.setEffectsVolume(value);
  162. cc.sys.localStorage.setItem("audio", JSON.stringify({ effect: this._effectVolume, music: this._musicVolume }));
  163. }
  164. /**
  165. * 获取音效大小
  166. * @return 0.0 - 1.0
  167. */
  168. public getEffectVolume() {
  169. return cc.audioEngine.getEffectsVolume();
  170. }
  171. /**
  172. * 恢复当前说暂停的所有音效
  173. */
  174. public setResumeAllEffect() {
  175. if (this._switchEffect) {
  176. cc.audioEngine.resumeAllEffects();
  177. }
  178. }
  179. /**
  180. * 停止播放所有正在播放的音效
  181. */
  182. public setStopAllEffect() {
  183. cc.audioEngine.stopAllEffects();
  184. }
  185. /** 暂时开启关闭音效开关
  186. * @param state true 打开 false 关闭
  187. */
  188. public setTempEffect(state) {
  189. this._switchTempEffect = state
  190. }
  191. /**
  192. * 背景音乐播放
  193. * url: 资源路径
  194. * loop: 是否循环
  195. */
  196. public playMusic(url, loop = false) {
  197. var self = this;
  198. cc.loader.loadRes('sounds/' + url, (err, cip) => {
  199. if (err) {
  200. console.error(err);
  201. }
  202. cc.audioEngine.playMusic(cip, loop);
  203. })
  204. }
  205. /**
  206. * 转换音乐按钮开关
  207. */
  208. public switchMusicFunc() {
  209. this._switchMusic = !this._switchMusic;
  210. if (!this._switchMusic) {
  211. // this.setPauseMusic();
  212. this.setStopMusic()
  213. if (this.IsMatePlayingEffect())
  214. this.pauseMateEffect()
  215. }
  216. else {
  217. // this.setResumeMusic();
  218. if (this.mateId != -1) {
  219. this.resumeMateEffect()
  220. } else {
  221. this.playMusic(AUDIO_TYPE.bg, true)
  222. }
  223. }
  224. cc.sys.localStorage.setItem("switchMusic", this._switchMusic.toString());
  225. }
  226. /**
  227. * 获取音乐开关状态
  228. */
  229. public getSwitchMusic() {
  230. return this._switchMusic;
  231. }
  232. /**
  233. * 暂停当前播放音乐
  234. */
  235. public setPauseMusic() {
  236. cc.audioEngine.pauseMusic();
  237. }
  238. /**
  239. * 恢复当前被暂停音乐音乐
  240. */
  241. public setResumeMusic() {
  242. if (this._switchMusic) {
  243. cc.audioEngine.resumeMusic();
  244. }
  245. }
  246. /**
  247. * 暂停播放音乐
  248. * releaseData: 控制是否释放音乐资源
  249. */
  250. public setStopMusic() {
  251. cc.audioEngine.stopMusic();
  252. }
  253. public setMusicVolume(value) {
  254. this._musicVolume = value;
  255. cc.audioEngine.setMusicVolume(value);
  256. cc.sys.localStorage.setItem("audio", JSON.stringify({ effect: this._effectVolume, music: this._musicVolume }));
  257. }
  258. public getMusicVolume() {
  259. return cc.audioEngine.getMusicVolume();
  260. }
  261. /**
  262. * 音乐是否正在播放(验证些方法来实现背景音乐是否播放完成)
  263. * return boolen
  264. */
  265. public isMusicPlaying() {
  266. return cc.audioEngine.isMusicPlaying();
  267. }
  268. /**
  269. * 释放指定音效资源
  270. * url
  271. */
  272. public releaseAudio(audio: cc.AudioClip) {
  273. if (audio) {
  274. cc.audioEngine.uncache(audio);
  275. }
  276. else {
  277. cc.error("【音频】资源" + audio + "不存在, 释放失败");
  278. }
  279. }
  280. public stopAllAudio() {
  281. cc.audioEngine.stopAll();
  282. }
  283. public releaseAllAudio() {
  284. cc.audioEngine.uncacheAll();
  285. }
  286. // update (dt) {}
  287. }