cf08a99cfaa5a43d2efd148a660311b4264f9fc3.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2", "__unresolved_3"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, AudioClip, Node, NodePool, oops, resLoader, AudioEffect, AudioEffectPool, _crd, AE_ID_MAX;
  4. function _reportPossibleCrUseOfoops(extras) {
  5. _reporterNs.report("oops", "../../Oops", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfresLoader(extras) {
  8. _reporterNs.report("resLoader", "../loader/ResLoader", _context.meta, extras);
  9. }
  10. function _reportPossibleCrUseOfAudioEffect(extras) {
  11. _reporterNs.report("AudioEffect", "./AudioEffect", _context.meta, extras);
  12. }
  13. _export("AudioEffectPool", 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. AudioClip = _cc.AudioClip;
  22. Node = _cc.Node;
  23. NodePool = _cc.NodePool;
  24. }, function (_unresolved_2) {
  25. oops = _unresolved_2.oops;
  26. }, function (_unresolved_3) {
  27. resLoader = _unresolved_3.resLoader;
  28. }, function (_unresolved_4) {
  29. AudioEffect = _unresolved_4.AudioEffect;
  30. }],
  31. execute: function () {
  32. _crd = true;
  33. _cclegacy._RF.push({}, "01278BDjrtCr4CBpmO5DZlN", "AudioEffectPool", undefined);
  34. __checkObsolete__(['AudioClip', 'Node', 'NodePool']);
  35. AE_ID_MAX = 30000;
  36. /** 音效池 */
  37. _export("AudioEffectPool", AudioEffectPool = class AudioEffectPool {
  38. constructor() {
  39. this._switch = true;
  40. this._volume = 1;
  41. /** 音效播放器对象池 */
  42. this.pool = new NodePool();
  43. /** 对象池集合 */
  44. this.effects = new Map();
  45. /** 用过的音效资源记录 */
  46. this.res = new Map();
  47. this._aeId = 0;
  48. }
  49. /** 音效开关 */
  50. get switch() {
  51. return this._switch;
  52. }
  53. set switch(value) {
  54. this._switch = value;
  55. if (value) this.stop();
  56. }
  57. /** 所有音效音量 */
  58. get volume() {
  59. return this._volume;
  60. }
  61. set volume(value) {
  62. this._volume = value;
  63. this.effects.forEach(ae => {
  64. ae.volume = value;
  65. });
  66. }
  67. /** 获取请求唯一编号 */
  68. getAeId() {
  69. if (this._aeId == AE_ID_MAX) this._aeId = 1;
  70. this._aeId++;
  71. return this._aeId;
  72. }
  73. /**
  74. * 加载与播放音效
  75. * @param url 音效资源地址与音效资源
  76. * @param bundleName 资源包名
  77. * @param onPlayComplete 播放完成回调
  78. * @returns
  79. */
  80. async load(url, bundleName = (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  81. error: Error()
  82. }), resLoader) : resLoader).defaultBundleName, onPlayComplete) {
  83. return new Promise(async (resolve, reject) => {
  84. if (!this.switch) return resolve(-1); // 创建音效资源
  85. let clip;
  86. if (url instanceof AudioClip) {
  87. clip = url;
  88. } else {
  89. clip = (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  90. error: Error()
  91. }), resLoader) : resLoader).get(url, AudioClip, bundleName);
  92. if (!clip) {
  93. this.res.set(bundleName, url);
  94. clip = await (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  95. error: Error()
  96. }), resLoader) : resLoader).loadAsync(bundleName, url, AudioClip);
  97. }
  98. } // 资源已被释放
  99. if (!clip.isValid) {
  100. resolve(-1);
  101. return;
  102. }
  103. let aeid = this.getAeId();
  104. let key;
  105. if (url instanceof AudioClip) {
  106. key = url.uuid;
  107. } else {
  108. key = `${bundleName}_${url}`;
  109. }
  110. key += "_" + aeid; // 获取音效果播放器播放音乐
  111. let ae;
  112. let node = null;
  113. if (this.pool.size() == 0) {
  114. node = new Node();
  115. node.name = "AudioEffect";
  116. node.parent = (_crd && oops === void 0 ? (_reportPossibleCrUseOfoops({
  117. error: Error()
  118. }), oops) : oops).audio.node;
  119. ae = node.addComponent(_crd && AudioEffect === void 0 ? (_reportPossibleCrUseOfAudioEffect({
  120. error: Error()
  121. }), AudioEffect) : AudioEffect);
  122. } else {
  123. node = this.pool.get();
  124. ae = node.getComponent(_crd && AudioEffect === void 0 ? (_reportPossibleCrUseOfAudioEffect({
  125. error: Error()
  126. }), AudioEffect) : AudioEffect);
  127. }
  128. ae.onComplete = () => {
  129. this.put(aeid, url, bundleName); // 播放完回收对象
  130. onPlayComplete && onPlayComplete(); // console.log(`【音效】回收,池中剩余音效播放器【${this.pool.size()}】`);
  131. }; // 记录正在播放的音效播放器
  132. this.effects.set(key, ae);
  133. ae.volume = this.volume;
  134. ae.clip = clip;
  135. ae.play();
  136. resolve(aeid);
  137. });
  138. }
  139. /**
  140. * 回收音效播放器
  141. * @param aeid 播放器编号
  142. * @param url 音效路径
  143. * @param bundleName 资源包名
  144. */
  145. put(aeid, url, bundleName = (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  146. error: Error()
  147. }), resLoader) : resLoader).defaultBundleName) {
  148. let key;
  149. if (url instanceof AudioClip) {
  150. key = url.uuid;
  151. } else {
  152. key = `${bundleName}_${url}`;
  153. }
  154. key += "_" + aeid;
  155. let ae = this.effects.get(key);
  156. if (ae && ae.clip) {
  157. this.effects.delete(key);
  158. ae.stop();
  159. this.pool.put(ae.node);
  160. }
  161. }
  162. /** 释放所有音效资源与对象池中播放器 */
  163. release() {
  164. // 释放正在播放的音效
  165. this.effects.forEach(ae => {
  166. ae.node.destroy();
  167. });
  168. this.effects.clear(); // 释放音效资源
  169. this.res.forEach((url, bundleName) => {
  170. (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  171. error: Error()
  172. }), resLoader) : resLoader).release(bundleName, url);
  173. }); // 释放池中播放器
  174. this.pool.clear();
  175. }
  176. /** 停止播放所有音效 */
  177. stop() {
  178. this.effects.forEach(ae => {
  179. ae.stop();
  180. });
  181. }
  182. /** 恢复所有音效 */
  183. play() {
  184. if (!this.switch) return;
  185. this.effects.forEach(ae => {
  186. ae.play();
  187. });
  188. }
  189. /** 暂停所有音效 */
  190. pause() {
  191. if (!this.switch) return;
  192. this.effects.forEach(ae => {
  193. ae.pause();
  194. });
  195. }
  196. });
  197. _cclegacy._RF.pop();
  198. _crd = false;
  199. }
  200. };
  201. });
  202. //# sourceMappingURL=cf08a99cfaa5a43d2efd148a660311b4264f9fc3.js.map