a7edfee9816ad8e83f26ded02ccedff9a7bda574.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2", "__unresolved_3"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Component, oops, AudioEffectPool, AudioMusic, AudioManager, _crd, LOCAL_STORE_KEY;
  4. function _reportPossibleCrUseOfoops(extras) {
  5. _reporterNs.report("oops", "../../Oops", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfAudioEffectPool(extras) {
  8. _reporterNs.report("AudioEffectPool", "./AudioEffectPool", _context.meta, extras);
  9. }
  10. function _reportPossibleCrUseOfAudioMusic(extras) {
  11. _reporterNs.report("AudioMusic", "./AudioMusic", _context.meta, extras);
  12. }
  13. _export("AudioManager", void 0);
  14. return {
  15. setters: [function (_unresolved_) {
  16. _reporterNs = _unresolved_;
  17. }, function (_cc) {
  18. _cclegacy = _cc.cclegacy;
  19. __checkObsolete__ = _cc.__checkObsolete__;
  20. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  21. Component = _cc.Component;
  22. }, function (_unresolved_2) {
  23. oops = _unresolved_2.oops;
  24. }, function (_unresolved_3) {
  25. AudioEffectPool = _unresolved_3.AudioEffectPool;
  26. }, function (_unresolved_4) {
  27. AudioMusic = _unresolved_4.AudioMusic;
  28. }],
  29. execute: function () {
  30. _crd = true;
  31. _cclegacy._RF.push({}, "252f0z+vPNL8Y/jsLYmomtw", "AudioManager", undefined);
  32. __checkObsolete__(['AudioClip', 'Component']);
  33. LOCAL_STORE_KEY = "game_audio";
  34. /**
  35. * 音频管理
  36. * @help https://gitee.com/dgflash/oops-framework/wikis/pages?sort_id=12037893&doc_id=2873565
  37. * @example
  38. // 模块功能通过 oops.audio 调用
  39. oops.audio.playMusic("audios/nocturne");
  40. */
  41. _export("AudioManager", AudioManager = class AudioManager extends Component {
  42. constructor() {
  43. super(...arguments);
  44. /** 背景音乐管理对象 */
  45. this.music = null;
  46. /** 音效管理对象 */
  47. this.effect = new (_crd && AudioEffectPool === void 0 ? (_reportPossibleCrUseOfAudioEffectPool({
  48. error: Error()
  49. }), AudioEffectPool) : AudioEffectPool)();
  50. /** 音乐管理状态数据 */
  51. this.local_data = {};
  52. }
  53. /**
  54. * 设置背景音乐播放完成回调
  55. * @param callback 背景音乐播放完成回调
  56. */
  57. setMusicComplete(callback) {
  58. if (callback === void 0) {
  59. callback = null;
  60. }
  61. this.music.onComplete = callback;
  62. }
  63. /**
  64. * 播放背景音乐
  65. * @param url 资源地址
  66. * @param callback 音乐播放完成事件
  67. * @param bundleName 资源包名
  68. */
  69. playMusic(url, callback, bundleName) {
  70. if (this.music.switch) {
  71. this.music.loop = false;
  72. this.music.load(url, callback, bundleName).then();
  73. }
  74. }
  75. /** 循环播放背景音乐 */
  76. playMusicLoop(url, bundleName) {
  77. if (this.music.switch) {
  78. this.music.loop = true;
  79. this.music.load(url, null, bundleName).then();
  80. }
  81. }
  82. /** 停止背景音乐播放 */
  83. stopMusic() {
  84. if (this.music.switch && this.music.playing) {
  85. this.music.stop();
  86. }
  87. }
  88. /**
  89. * 获取背景音乐播放进度
  90. */
  91. get progressMusic() {
  92. return this.music.progress;
  93. }
  94. /**
  95. * 设置背景乐播放进度
  96. * @param value 播放进度值
  97. */
  98. set progressMusic(value) {
  99. this.music.progress = value;
  100. }
  101. /**
  102. * 获取背景音乐音量
  103. */
  104. get volumeMusic() {
  105. return this.music.volume;
  106. }
  107. /**
  108. * 设置背景音乐音量
  109. * @param value 音乐音量值
  110. */
  111. set volumeMusic(value) {
  112. this.music.volume = value;
  113. this.save();
  114. }
  115. /**
  116. * 获取背景音乐开关值
  117. */
  118. get switchMusic() {
  119. return this.music.switch;
  120. }
  121. /**
  122. * 设置背景音乐开关值
  123. * @param value 开关值
  124. */
  125. set switchMusic(value) {
  126. this.music.switch = value;
  127. if (!value) this.music.stop();
  128. this.save();
  129. }
  130. /**
  131. * 播放音效
  132. * @param url 资源地址
  133. * @param callback 加载完成回调
  134. * @param bundleName 资源包名
  135. */
  136. playEffect(url, bundleName, onPlayComplete) {
  137. return this.effect.load(url, bundleName, onPlayComplete);
  138. }
  139. /** 回收音效播放器 */
  140. putEffect(aeid, url, bundleName) {
  141. this.effect.put(aeid, url, bundleName);
  142. }
  143. /** 获取音效音量 */
  144. get volumeEffect() {
  145. return this.effect.volume;
  146. }
  147. /**
  148. * 设置获取音效音量
  149. * @param value 音效音量值
  150. */
  151. set volumeEffect(value) {
  152. this.effect.volume = value;
  153. this.save();
  154. }
  155. /** 获取音效开关值 */
  156. get switchEffect() {
  157. return this.effect.switch;
  158. }
  159. /**
  160. * 设置音效开关值
  161. * @param value 音效开关值
  162. */
  163. set switchEffect(value) {
  164. this.effect.switch = value;
  165. if (!value) this.effect.stop();
  166. this.save();
  167. }
  168. /** 恢复当前暂停的音乐与音效播放 */
  169. resumeAll() {
  170. if (!this.music.playing && this.music.progress > 0) this.music.play();
  171. this.effect.play();
  172. }
  173. /** 暂停当前音乐与音效的播放 */
  174. pauseAll() {
  175. if (this.music.playing) this.music.pause();
  176. this.effect.pause();
  177. }
  178. /** 停止当前音乐与音效的播放 */
  179. stopAll() {
  180. this.music.stop();
  181. this.effect.stop();
  182. }
  183. /** 保存音乐音效的音量、开关配置数据到本地 */
  184. save() {
  185. this.local_data.volume_music = this.music.volume;
  186. this.local_data.volume_effect = this.effect.volume;
  187. this.local_data.switch_music = this.music.switch;
  188. this.local_data.switch_effect = this.effect.switch;
  189. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  190. error: Error()
  191. }), oops) : oops).storage.set(LOCAL_STORE_KEY, this.local_data);
  192. }
  193. /** 本地加载音乐音效的音量、开关配置数据并设置到游戏中 */
  194. load() {
  195. this.music = this.getComponent(_crd && AudioMusic === void 0 ? (_reportPossibleCrUseOfAudioMusic({
  196. error: Error()
  197. }), AudioMusic) : AudioMusic) || this.addComponent(_crd && AudioMusic === void 0 ? (_reportPossibleCrUseOfAudioMusic({
  198. error: Error()
  199. }), AudioMusic) : AudioMusic);
  200. this.local_data = (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  201. error: Error()
  202. }), oops) : oops).storage.getJson(LOCAL_STORE_KEY);
  203. if (this.local_data) {
  204. try {
  205. this.setState();
  206. } catch (_unused) {
  207. this.setStateDefault();
  208. }
  209. } else {
  210. this.setStateDefault();
  211. }
  212. }
  213. setState() {
  214. this.music.volume = this.local_data.volume_music;
  215. this.effect.volume = this.local_data.volume_effect;
  216. this.music.switch = this.local_data.switch_music;
  217. this.effect.switch = this.local_data.switch_effect;
  218. }
  219. setStateDefault() {
  220. this.local_data = {};
  221. this.music.volume = 1;
  222. this.effect.volume = 1;
  223. this.music.switch = true;
  224. this.effect.switch = true;
  225. }
  226. });
  227. _cclegacy._RF.pop();
  228. _crd = false;
  229. }
  230. };
  231. });
  232. //# sourceMappingURL=a7edfee9816ad8e83f26ded02ccedff9a7bda574.js.map