a7edfee9816ad8e83f26ded02ccedff9a7bda574.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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(...args) {
  43. super(...args);
  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 = null) {
  58. this.music.onComplete = callback;
  59. }
  60. /**
  61. * 播放背景音乐
  62. * @param url 资源地址
  63. * @param callback 音乐播放完成事件
  64. * @param bundleName 资源包名
  65. */
  66. playMusic(url, callback, bundleName) {
  67. if (this.music.switch) {
  68. this.music.loop = false;
  69. this.music.load(url, callback, bundleName).then();
  70. }
  71. }
  72. /** 循环播放背景音乐 */
  73. playMusicLoop(url, bundleName) {
  74. if (this.music.switch) {
  75. this.music.loop = true;
  76. this.music.load(url, null, bundleName).then();
  77. }
  78. }
  79. /** 停止背景音乐播放 */
  80. stopMusic() {
  81. if (this.music.switch && this.music.playing) {
  82. this.music.stop();
  83. }
  84. }
  85. /**
  86. * 获取背景音乐播放进度
  87. */
  88. get progressMusic() {
  89. return this.music.progress;
  90. }
  91. /**
  92. * 设置背景乐播放进度
  93. * @param value 播放进度值
  94. */
  95. set progressMusic(value) {
  96. this.music.progress = value;
  97. }
  98. /**
  99. * 获取背景音乐音量
  100. */
  101. get volumeMusic() {
  102. return this.music.volume;
  103. }
  104. /**
  105. * 设置背景音乐音量
  106. * @param value 音乐音量值
  107. */
  108. set volumeMusic(value) {
  109. this.music.volume = value;
  110. this.save();
  111. }
  112. /**
  113. * 获取背景音乐开关值
  114. */
  115. get switchMusic() {
  116. return this.music.switch;
  117. }
  118. /**
  119. * 设置背景音乐开关值
  120. * @param value 开关值
  121. */
  122. set switchMusic(value) {
  123. this.music.switch = value;
  124. if (!value) this.music.stop();
  125. this.save();
  126. }
  127. /**
  128. * 播放音效
  129. * @param url 资源地址
  130. * @param callback 加载完成回调
  131. * @param bundleName 资源包名
  132. */
  133. playEffect(url, bundleName, onPlayComplete) {
  134. return this.effect.load(url, bundleName, onPlayComplete);
  135. }
  136. /** 回收音效播放器 */
  137. putEffect(aeid, url, bundleName) {
  138. this.effect.put(aeid, url, bundleName);
  139. }
  140. /** 获取音效音量 */
  141. get volumeEffect() {
  142. return this.effect.volume;
  143. }
  144. /**
  145. * 设置获取音效音量
  146. * @param value 音效音量值
  147. */
  148. set volumeEffect(value) {
  149. this.effect.volume = value;
  150. this.save();
  151. }
  152. /** 获取音效开关值 */
  153. get switchEffect() {
  154. return this.effect.switch;
  155. }
  156. /**
  157. * 设置音效开关值
  158. * @param value 音效开关值
  159. */
  160. set switchEffect(value) {
  161. this.effect.switch = value;
  162. if (!value) this.effect.stop();
  163. this.save();
  164. }
  165. /** 恢复当前暂停的音乐与音效播放 */
  166. resumeAll() {
  167. if (!this.music.playing && this.music.progress > 0) this.music.play();
  168. this.effect.play();
  169. }
  170. /** 暂停当前音乐与音效的播放 */
  171. pauseAll() {
  172. if (this.music.playing) this.music.pause();
  173. this.effect.pause();
  174. }
  175. /** 停止当前音乐与音效的播放 */
  176. stopAll() {
  177. this.music.stop();
  178. this.effect.stop();
  179. }
  180. /** 保存音乐音效的音量、开关配置数据到本地 */
  181. save() {
  182. this.local_data.volume_music = this.music.volume;
  183. this.local_data.volume_effect = this.effect.volume;
  184. this.local_data.switch_music = this.music.switch;
  185. this.local_data.switch_effect = this.effect.switch;
  186. (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  187. error: Error()
  188. }), oops) : oops).storage.set(LOCAL_STORE_KEY, this.local_data);
  189. }
  190. /** 本地加载音乐音效的音量、开关配置数据并设置到游戏中 */
  191. load() {
  192. this.music = this.getComponent(_crd && AudioMusic === void 0 ? (_reportPossibleCrUseOfAudioMusic({
  193. error: Error()
  194. }), AudioMusic) : AudioMusic) || this.addComponent(_crd && AudioMusic === void 0 ? (_reportPossibleCrUseOfAudioMusic({
  195. error: Error()
  196. }), AudioMusic) : AudioMusic);
  197. this.local_data = (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  198. error: Error()
  199. }), oops) : oops).storage.getJson(LOCAL_STORE_KEY);
  200. if (this.local_data) {
  201. try {
  202. this.setState();
  203. } catch {
  204. this.setStateDefault();
  205. }
  206. } else {
  207. this.setStateDefault();
  208. }
  209. }
  210. setState() {
  211. this.music.volume = this.local_data.volume_music;
  212. this.effect.volume = this.local_data.volume_effect;
  213. this.music.switch = this.local_data.switch_music;
  214. this.effect.switch = this.local_data.switch_effect;
  215. }
  216. setStateDefault() {
  217. this.local_data = {};
  218. this.music.volume = 1;
  219. this.effect.volume = 1;
  220. this.music.switch = true;
  221. this.effect.switch = true;
  222. }
  223. });
  224. _cclegacy._RF.pop();
  225. _crd = false;
  226. }
  227. };
  228. });
  229. //# sourceMappingURL=a7edfee9816ad8e83f26ded02ccedff9a7bda574.js.map