e908646c1435d653de6447e2ae846c959ac29b33.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, assetManager, AudioClip, AudioSource, director, Node, ch_audio, _crd, ch_log, ch_storage, loadType;
  4. _export("default", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. __checkObsolete__ = _cc.__checkObsolete__;
  9. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  10. assetManager = _cc.assetManager;
  11. AudioClip = _cc.AudioClip;
  12. AudioSource = _cc.AudioSource;
  13. director = _cc.director;
  14. Node = _cc.Node;
  15. }],
  16. execute: function () {
  17. _crd = true;
  18. _cclegacy._RF.push({}, "e1803S2AupKB5LQvJ7w31/E", "audio", undefined);
  19. __checkObsolete__(['assetManager', 'AudioClip', 'AudioSource', 'director', 'Node']);
  20. ch_log = chsdk.log;
  21. ch_storage = chsdk.storage;
  22. /**音频等资源加载方式*/
  23. _export("loadType", loadType = /*#__PURE__*/function (loadType) {
  24. loadType[loadType["none"] = 0] = "none";
  25. loadType[loadType["bundle"] = 1] = "bundle";
  26. loadType[loadType["remote"] = 2] = "remote";
  27. return loadType;
  28. }({}));
  29. /**音频播放控制
  30. * 需要初始化资源加载方式
  31. * loadType.bundle 时需要设置 bundle名
  32. * loadType.remote 测试使用远程的音频需要设置远程地址
  33. */
  34. _export("default", ch_audio = class ch_audio {
  35. static getInstance() {
  36. if (!this._instance) this._instance = new ch_audio();
  37. return this._instance;
  38. }
  39. constructor() {
  40. this._volume_music = 1;
  41. this._volume_effect = 1;
  42. this._switch_music = true;
  43. this._switch_effect = true;
  44. this._effect_max = 5;
  45. this._effect_index = 0;
  46. this._effect_source_pool = [];
  47. this._music_source = void 0;
  48. this._load_type = loadType.none;
  49. this._bundle_name = void 0;
  50. this._remote_url = void 0;
  51. this._playing_sound = new Set();
  52. var audio = new Node();
  53. audio.name = '__ch_audio__';
  54. director.getScene().addChild(audio);
  55. director.addPersistRootNode(audio);
  56. this._music_source = this._create(audio);
  57. for (var i = 0; i < this._effect_max; i++) {
  58. this._effect_source_pool.push(this._create(audio));
  59. }
  60. this.load();
  61. }
  62. /**
  63. * 创建音频源
  64. * @param node 节点
  65. * @param volume 音量
  66. * @returns AudioSource 音频源组件
  67. */
  68. _create(node) {
  69. var source = node.addComponent(AudioSource);
  70. source.loop = false;
  71. source.playOnAwake = false;
  72. source.volume = 0.5;
  73. return source;
  74. }
  75. /**初始化*/
  76. init(load_type, bundle_name, remote_url) {
  77. this._load_type = load_type;
  78. this._bundle_name = bundle_name;
  79. this._remote_url = remote_url;
  80. }
  81. /**切换bundle*/
  82. set_bundle_name(bundle_name) {
  83. this._bundle_name = bundle_name;
  84. }
  85. /**
  86. * 释放通过 [[load]] 或者 [[loadDir]] 加载的声音资源。
  87. * @param sound 声音资源路径
  88. */
  89. release(sound) {
  90. if (this._load_type == loadType.none) {
  91. ch_log.warn('音频模块未初始化');
  92. } else if (this._load_type == loadType.bundle) {
  93. var bundle = assetManager.getBundle(this._bundle_name);
  94. if (!sound) {
  95. bundle.releaseAll();
  96. } else {
  97. bundle.release(sound, AudioClip);
  98. }
  99. }
  100. }
  101. /** 保存音乐音效的音量、开关配置数据到本地 */
  102. save() {
  103. var local_data = {};
  104. local_data.volume_music = this._volume_music;
  105. local_data.volume_effect = this._volume_effect;
  106. local_data.switch_music = this._switch_music;
  107. local_data.switch_effect = this._switch_effect;
  108. ch_storage.set("ch_audio", local_data);
  109. }
  110. /** 本地加载音乐音效的音量、开关配置数据并设置到游戏中 */
  111. load() {
  112. var local_data = ch_storage.getObject("ch_audio");
  113. if (local_data) {
  114. try {
  115. this.setState(local_data);
  116. } catch (e) {
  117. this.setStateDefault();
  118. }
  119. } else {
  120. this.setStateDefault();
  121. }
  122. }
  123. setState(local_data) {
  124. this.volumeMusic = local_data.volume_music;
  125. this.volumeEffect = local_data.volume_effect;
  126. this.switchMusic = local_data.switch_music;
  127. this.switchEffect = local_data.switch_effect;
  128. }
  129. setStateDefault() {
  130. this.volumeMusic = 0.8;
  131. this.volumeEffect = 0.8;
  132. this.switchMusic = true;
  133. this.switchEffect = true;
  134. }
  135. /**
  136. * 获取背景音乐音量
  137. */
  138. get volumeMusic() {
  139. return this._volume_music;
  140. }
  141. /**
  142. * 设置背景音乐音量
  143. * @param value 音乐音量值
  144. */
  145. set volumeMusic(value) {
  146. this._volume_music = value;
  147. this._music_source.volume = value;
  148. }
  149. /**
  150. * 获取背景音乐开关值
  151. */
  152. get switchMusic() {
  153. return this._switch_music;
  154. }
  155. /**
  156. * 设置背景音乐开关值
  157. * @param value 开关值
  158. */
  159. set switchMusic(value) {
  160. this._switch_music = value;
  161. if (value == false) this._music_source.stop();
  162. }
  163. /**
  164. * 获取音效音量
  165. */
  166. get volumeEffect() {
  167. return this._volume_effect;
  168. }
  169. /**
  170. * 设置获取音效音量
  171. * @param value 音效音量值
  172. */
  173. set volumeEffect(value) {
  174. this._volume_effect = value;
  175. for (var i = 0; i < this._effect_source_pool.length; i++) {
  176. this._effect_source_pool[i].volume = this._volume_effect;
  177. }
  178. }
  179. /**
  180. * 获取音效开关值
  181. */
  182. get switchEffect() {
  183. return this._switch_effect;
  184. }
  185. /**
  186. * 设置音效开关值
  187. * @param value 音效开关值
  188. */
  189. set switchEffect(value) {
  190. this._switch_effect = value;
  191. if (value == false) {
  192. for (var i = 0; i < this._effect_source_pool.length; i++) {
  193. this._effect_source_pool[i].stop();
  194. }
  195. }
  196. }
  197. /**
  198. * @en
  199. * play short audio, such as strikes,explosions
  200. * @zh
  201. * 播放短音频,比如 打击音效,爆炸音效等
  202. * @param sound clip or url for the audio
  203. * @param interval 同名字音频限制播放间隔(毫秒) (默认:0不限制 特殊系数:>0 <=1 使用音频时间X此系数)
  204. */
  205. playOneShot(sound, interval, remote_ext) {
  206. if (interval === void 0) {
  207. interval = 0;
  208. }
  209. if (remote_ext === void 0) {
  210. remote_ext = '.mp3';
  211. }
  212. if (!this._switch_effect) return;
  213. if (sound instanceof AudioClip) {
  214. this.doPlayOneShot(sound, interval);
  215. } else {
  216. if (this._load_type == loadType.none) {
  217. ch_log.warn('音频模块未初始化');
  218. } else if (this._load_type == loadType.bundle) {
  219. var bundle = assetManager.getBundle(this._bundle_name);
  220. if (!bundle) {
  221. ch_log.warn("\u8BF7\u786E\u4FDD bundle" + this._bundle_name + " \u5DF2\u52A0\u8F7D");
  222. } else {
  223. bundle.load(sound, (err, clip) => {
  224. if (err) {
  225. ch_log.error(err);
  226. } else {
  227. this.doPlayOneShot(clip, interval);
  228. }
  229. });
  230. }
  231. } else if (this._load_type == loadType.remote) {
  232. assetManager.loadRemote(this._remote_url + sound + remote_ext, (err, clip) => {
  233. if (err) {
  234. ch_log.error(err);
  235. } else {
  236. this.doPlayOneShot(clip, interval);
  237. }
  238. });
  239. }
  240. }
  241. }
  242. doPlayOneShot(clip, interval) {
  243. var name = clip.name;
  244. if (interval > 0) {
  245. if (this._playing_sound.has(name)) return;
  246. this._playing_sound.add(name);
  247. var time = interval <= 1 ? clip.getDuration() * interval * 1000 : interval;
  248. setTimeout(() => {
  249. this._playing_sound.delete(name);
  250. }, time);
  251. this.getNextEffectSource().playOneShot(clip, this._volume_effect);
  252. } else {
  253. this.getNextEffectSource().playOneShot(clip, this._volume_effect);
  254. }
  255. }
  256. getNextEffectSource() {
  257. var source = this._effect_source_pool[this._effect_index];
  258. this._effect_index = (this._effect_index + 1) % this._effect_max;
  259. return source;
  260. }
  261. /**
  262. * @en
  263. * play long audio, such as the bg music
  264. * @zh
  265. * 播放长音频,比如 背景音乐
  266. * @param sound clip or url for the sound
  267. */
  268. play(sound, remote_ext) {
  269. if (remote_ext === void 0) {
  270. remote_ext = '.mp3';
  271. }
  272. if (!this._switch_music) return;
  273. if (sound instanceof AudioClip) {
  274. this._music_source.loop = true;
  275. this._music_source.stop();
  276. this._music_source.clip = sound;
  277. this._music_source.play();
  278. this._music_source.volume = this._volume_music;
  279. } else {
  280. if (this._load_type == loadType.none) {
  281. ch_log.warn('音频模块未初始化');
  282. } else if (this._load_type == loadType.bundle) {
  283. var bundle = assetManager.getBundle(this._bundle_name);
  284. if (!bundle) {
  285. ch_log.warn("\u8BF7\u786E\u4FDD bundle" + this._bundle_name + " \u5DF2\u52A0\u8F7D");
  286. } else {
  287. bundle.load(sound, (err, clip) => {
  288. if (err) {
  289. ch_log.error(err);
  290. } else {
  291. this._music_source.loop = true;
  292. this._music_source.stop();
  293. this._music_source.clip = clip;
  294. this._music_source.play();
  295. this._music_source.volume = this._volume_music;
  296. }
  297. });
  298. }
  299. } else if (this._load_type == loadType.remote) {
  300. assetManager.loadRemote(this._remote_url + sound + remote_ext, (err, clip) => {
  301. if (err) {
  302. ch_log.error(err);
  303. } else {
  304. this._music_source.loop = true;
  305. this._music_source.stop();
  306. this._music_source.clip = clip;
  307. this._music_source.play();
  308. this._music_source.volume = this._volume_music;
  309. }
  310. });
  311. }
  312. }
  313. }
  314. /**
  315. * stop the audio play
  316. */
  317. stop() {
  318. this._music_source.stop();
  319. for (var i = 0; i < this._effect_source_pool.length; i++) {
  320. this._effect_source_pool[i].stop();
  321. }
  322. }
  323. /**stop and clean */
  324. clean() {
  325. var _this$_music_source$c;
  326. this._music_source.stop();
  327. (_this$_music_source$c = this._music_source.clip) == null || _this$_music_source$c.name;
  328. this._music_source.clip = null;
  329. for (var i = 0; i < this._effect_source_pool.length; i++) {
  330. this._effect_source_pool[i].stop();
  331. this._effect_source_pool[i].clip = null;
  332. }
  333. }
  334. /**
  335. * pause the audio play
  336. */
  337. pause() {
  338. this._music_source.pause();
  339. }
  340. /**
  341. * resume the audio play
  342. */
  343. resume() {
  344. if (!this._switch_music) return;
  345. this._music_source.play();
  346. }
  347. /** 重播当前音乐 */
  348. replay_music() {
  349. this._music_source.stop();
  350. this._music_source.play();
  351. }
  352. });
  353. ch_audio._instance = void 0;
  354. _cclegacy._RF.pop();
  355. _crd = false;
  356. }
  357. };
  358. });
  359. //# sourceMappingURL=e908646c1435d653de6447e2ae846c959ac29b33.js.map