5e13f36f4421b76707e6eeb60ecf1fdbf3756df9.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. System.register(["__unresolved_0", "cc", "__unresolved_1"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, AudioClip, AudioSource, _decorator, resLoader, _dec, _class, _crd, ccclass, menu, AudioMusic;
  4. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  5. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  6. function _reportPossibleCrUseOfresLoader(extras) {
  7. _reporterNs.report("resLoader", "../loader/ResLoader", _context.meta, extras);
  8. }
  9. return {
  10. setters: [function (_unresolved_) {
  11. _reporterNs = _unresolved_;
  12. }, function (_cc) {
  13. _cclegacy = _cc.cclegacy;
  14. __checkObsolete__ = _cc.__checkObsolete__;
  15. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  16. AudioClip = _cc.AudioClip;
  17. AudioSource = _cc.AudioSource;
  18. _decorator = _cc._decorator;
  19. }, function (_unresolved_2) {
  20. resLoader = _unresolved_2.resLoader;
  21. }],
  22. execute: function () {
  23. _crd = true;
  24. _cclegacy._RF.push({}, "5c1f3kqGetBiIv48/CvuaQv", "AudioMusic", undefined);
  25. /*
  26. * @Author: dgflash
  27. * @Date: 2022-06-21 12:05:13
  28. * @LastEditors: dgflash
  29. * @LastEditTime: 2023-05-16 09:11:30
  30. */
  31. __checkObsolete__(['AudioClip', 'AudioSource', '_decorator']);
  32. ({
  33. ccclass,
  34. menu
  35. } = _decorator);
  36. /**
  37. * 背景音乐
  38. * 1、播放一个新背景音乐时,先加载音乐资源,然后停止正在播放的背景资源同时施放当前背景音乐资源,最后播放新的背景音乐
  39. */
  40. _export("AudioMusic", AudioMusic = (_dec = ccclass('AudioMusic'), _dec(_class = class AudioMusic extends AudioSource {
  41. constructor() {
  42. super(...arguments);
  43. /** 背景音乐开关 */
  44. this.switch = true;
  45. /** 背景音乐播放完成回调 */
  46. this.onComplete = null;
  47. this._progress = 0;
  48. this._isLoading = false;
  49. this._nextBundleName = null;
  50. // 下一个音乐资源包
  51. this._nextUrl = null;
  52. }
  53. // 下一个播放音乐
  54. start() {
  55. // this.node.on(AudioSource.EventType.STARTED, this.onAudioStarted, this);
  56. this.node.on(AudioSource.EventType.ENDED, this.onAudioEnded, this);
  57. } // private onAudioStarted() { }
  58. onAudioEnded() {
  59. this.onComplete && this.onComplete();
  60. }
  61. /** 获取音乐播放进度 */
  62. get progress() {
  63. if (this.duration > 0) this._progress = this.currentTime / this.duration;
  64. return this._progress;
  65. }
  66. /**
  67. * 设置音乐当前播放进度
  68. * @param value 进度百分比0到1之间
  69. */
  70. set progress(value) {
  71. this._progress = value;
  72. this.currentTime = value * this.duration;
  73. }
  74. /**
  75. * 加载音乐并播放
  76. * @param url 音乐资源地址
  77. * @param callback 加载完成回调
  78. * @param bundleName 资源包名
  79. */
  80. load(url, callback, bundleName) {
  81. var _this = this;
  82. return _asyncToGenerator(function* () {
  83. if (bundleName === void 0) {
  84. bundleName = (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  85. error: Error()
  86. }), resLoader) : resLoader).defaultBundleName;
  87. }
  88. // 下一个加载的背景音乐资源
  89. if (_this._isLoading) {
  90. _this._nextBundleName = bundleName;
  91. _this._nextUrl = url;
  92. return;
  93. }
  94. _this._isLoading = true;
  95. var data = yield (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  96. error: Error()
  97. }), resLoader) : resLoader).loadAsync(bundleName, url, AudioClip);
  98. if (data) {
  99. _this._isLoading = false; // 处理等待加载的背景音乐
  100. if (_this._nextUrl != null) {
  101. // 加载等待播放的背景音乐
  102. _this.load(_this._nextUrl, callback, _this._nextBundleName);
  103. _this._nextBundleName = _this._nextUrl = null;
  104. } else {
  105. callback && callback(); // 正在播放的时候先关闭
  106. if (_this.playing) {
  107. _this.stop();
  108. } // 删除当前正在播放的音乐
  109. _this.release(); // 播放背景音乐
  110. _this.clip = data;
  111. _this.play();
  112. }
  113. }
  114. })();
  115. }
  116. /** 释放当前背景音乐资源 */
  117. release() {
  118. if (this.clip) {
  119. this.stop();
  120. this.clip.decRef();
  121. this.clip = null;
  122. }
  123. }
  124. }) || _class));
  125. _cclegacy._RF.pop();
  126. _crd = false;
  127. }
  128. };
  129. });
  130. //# sourceMappingURL=5e13f36f4421b76707e6eeb60ecf1fdbf3756df9.js.map