System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2", "__unresolved_3", "__unresolved_4", "__unresolved_5", "__unresolved_6"], function (_export, _context) {
  "use strict";

  var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, AudioClip, AudioSource, Singleton, bundleMgr, GameConst, Logger, Utils, settingData, _dec, _class, _crd, ccclass, property, AudioManager, audioMgr;

  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); } }

  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); }); }; }

  function _reportPossibleCrUseOfSingleton(extras) {
    _reporterNs.report("Singleton", "./Singleton", _context.meta, extras);
  }

  function _reportPossibleCrUseOfbundleMgr(extras) {
    _reporterNs.report("bundleMgr", "./BundleManager", _context.meta, extras);
  }

  function _reportPossibleCrUseOfGameConst(extras) {
    _reporterNs.report("GameConst", "../common/GameConst", _context.meta, extras);
  }

  function _reportPossibleCrUseOfLogger(extras) {
    _reporterNs.report("Logger", "../extend/Logger", _context.meta, extras);
  }

  function _reportPossibleCrUseOfUtils(extras) {
    _reporterNs.report("Utils", "../utils/Utils", _context.meta, extras);
  }

  function _reportPossibleCrUseOfsettingData(extras) {
    _reporterNs.report("settingData", "../../user/SettingData", _context.meta, extras);
  }

  return {
    setters: [function (_unresolved_) {
      _reporterNs = _unresolved_;
    }, function (_cc) {
      _cclegacy = _cc.cclegacy;
      __checkObsolete__ = _cc.__checkObsolete__;
      __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
      _decorator = _cc._decorator;
      AudioClip = _cc.AudioClip;
      AudioSource = _cc.AudioSource;
    }, function (_unresolved_2) {
      Singleton = _unresolved_2.Singleton;
    }, function (_unresolved_3) {
      bundleMgr = _unresolved_3.bundleMgr;
    }, function (_unresolved_4) {
      GameConst = _unresolved_4.GameConst;
    }, function (_unresolved_5) {
      Logger = _unresolved_5.Logger;
    }, function (_unresolved_6) {
      Utils = _unresolved_6.default;
    }, function (_unresolved_7) {
      settingData = _unresolved_7.settingData;
    }],
    execute: function () {
      _crd = true;

      _cclegacy._RF.push({}, "f6afaklIZ9NTJV4lH+gPg3p", "AudioManager", undefined);

      __checkObsolete__(['_decorator', 'AudioClip', 'AudioSource']);

      ({
        ccclass,
        property
      } = _decorator);
      /**音乐播放数据结构*/

      AudioManager = (_dec = ccclass('AudioManager'), _dec(_class = class AudioManager extends (_crd && Singleton === void 0 ? (_reportPossibleCrUseOfSingleton({
        error: Error()
      }), Singleton) : Singleton) {
        constructor() {
          super(...arguments);
          //缓存的音乐
          this.cachedAudioArr = [];

          /**
           * 播放音效 不会两个相同的音乐重复播放 可以停止
           * @param path 播放音乐的路径
           * @param loop 是否循环播放
           * @param s 如果为-1则最大音量立即播放 如果为其他则慢慢音量递增起来播放
           * @returns 
           */
          this.bgVolumeMax = 0.8;
        }

        /**
         * 预加载音乐 
         * 这儿前面 bundleManager资源包已经 loadLaunchBundles加载过了
         * 可以直接 再次
         */
        initialize() {
          var _this = this;

          return _asyncToGenerator(function* () {
            var audioAssets = Object.entries((_crd && GameConst === void 0 ? (_reportPossibleCrUseOfGameConst({
              error: Error()
            }), GameConst) : GameConst).audios).map(_ref => {
              var [key, path] = _ref;
              return {
                path: path,
                type: AudioClip
              };
            }).filter(asset => !!asset.path);
            yield (_crd && bundleMgr === void 0 ? (_reportPossibleCrUseOfbundleMgr({
              error: Error()
            }), bundleMgr) : bundleMgr).loadMultipleAssets((_crd && GameConst === void 0 ? (_reportPossibleCrUseOfGameConst({
              error: Error()
            }), GameConst) : GameConst).bundleName.audios, audioAssets, progress => {
              /*进度条0~1*/
            }, (err, clip, path) => {
              if (err) {
                (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
                  error: Error()
                }), Logger) : Logger).error("加载资源失败:", err);
              } else {
                _this.cachedAudioArr.push({
                  clip: clip,
                  audioSource: null,
                  path: path,
                  playStatus: false,
                  totalTime: clip.getDuration(),
                  time_id: -1,
                  t: 0
                });
              }
            });
          })();
        }

        play(path, loop, s) {
          var _this2 = this;

          return _asyncToGenerator(function* () {
            if (loop === void 0) {
              loop = false;
            }

            if (s === void 0) {
              s = -1;
            }

            if ((_crd && Utils === void 0 ? (_reportPossibleCrUseOfUtils({
              error: Error()
            }), Utils) : Utils).isNull(path) || !(_crd && settingData === void 0 ? (_reportPossibleCrUseOfsettingData({
              error: Error()
            }), settingData) : settingData).data.bgMusic) return;

            var fun = audio => {
              if (audio.audioSource == null) {
                audio.audioSource = _this2.audioSourceLoop(loop);
              }

              if (audio.playStatus) return;
              audio.audioSource.clip = audio.clip;

              if (audio.time_id) {
                clearInterval(audio.time_id);

                if (audio.audioSource.playing) {
                  audio.audioSource.pause();
                }
              }

              audio.audioSource.play();

              if (s == -1) {
                audio.audioSource.volume = _this2.bgVolumeMax;
              } else {
                audio.t = 0;
                audio.audioSource.volume = 0;
                var interval = 1 / s / 10;
                audio.time_id = setInterval(() => {
                  audio.t += interval;
                  var volume = audio.t / s;

                  if (volume >= _this2.bgVolumeMax) {
                    clearInterval(audio.time_id);
                  } else {
                    audio.audioSource.volume = volume;
                  }
                }, interval * 1000);
              }

              audio.playStatus = true;
            };

            var a = _this2.cachedAudioArr.find(e => e.path == path);

            if (a) {
              fun(a);
            } else {
              yield (_crd && bundleMgr === void 0 ? (_reportPossibleCrUseOfbundleMgr({
                error: Error()
              }), bundleMgr) : bundleMgr).loadAsset((_crd && GameConst === void 0 ? (_reportPossibleCrUseOfGameConst({
                error: Error()
              }), GameConst) : GameConst).bundleName.audios, path, AudioClip, (err, clip) => {
                if (err) {
                  (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
                    error: Error()
                  }), Logger) : Logger).error("加载资源失败:", err);
                } else {
                  var data = {
                    path: path,
                    clip: clip,
                    audioSource: null,
                    playStatus: false,
                    totalTime: clip.getDuration(),
                    time_id: -1,
                    t: 0
                  };

                  _this2.cachedAudioArr.push(data);

                  fun(data);
                }
              });
            }
          })();
        }
        /**
         * 单次播放一次的音效 可以停止
         * @param path 音乐路径
         * @param overlap 是否可以重复两个同一个音效连续播放
         * @returns 
         */


        playOneShot(path, overlap) {
          var _this3 = this;

          return _asyncToGenerator(function* () {
            if (overlap === void 0) {
              overlap = true;
            }

            if ((_crd && Utils === void 0 ? (_reportPossibleCrUseOfUtils({
              error: Error()
            }), Utils) : Utils).isNull(path) || !(_crd && settingData === void 0 ? (_reportPossibleCrUseOfsettingData({
              error: Error()
            }), settingData) : settingData).data.soundFx) return;

            var fun = audio => {
              if (overlap || !audio.playStatus) {
                if (audio.audioSource == null) {
                  audio.audioSource = _this3.audioSourceLoop(false);
                }

                audio.audioSource.clip = audio.clip;
                audio.audioSource.play();
                audio.audioSource.volume = 1;
                audio.playStatus = true;
                setTimeout(() => {
                  audio.playStatus = false;
                }, audio.totalTime * 1000);
              }
            };

            var a = _this3.cachedAudioArr.find(e => e.path == path);

            if (a) {
              fun(a);
            } else {
              yield (_crd && bundleMgr === void 0 ? (_reportPossibleCrUseOfbundleMgr({
                error: Error()
              }), bundleMgr) : bundleMgr).loadAsset((_crd && GameConst === void 0 ? (_reportPossibleCrUseOfGameConst({
                error: Error()
              }), GameConst) : GameConst).bundleName.audios, path, AudioClip, (err, clip) => {
                if (err) {
                  (_crd && Logger === void 0 ? (_reportPossibleCrUseOfLogger({
                    error: Error()
                  }), Logger) : Logger).error("加载资源失败:", err);
                } else {
                  var data = {
                    path: path,
                    clip: clip,
                    audioSource: null,
                    playStatus: false,
                    totalTime: clip.getDuration(),
                    time_id: -1,
                    t: 0
                  };

                  _this3.cachedAudioArr.push(data);

                  fun(data);
                }
              });
            }
          })();
        }
        /**
         * 停止播放音效
         * @param path 音效路径名称
         * @param s 为-1立即停止播放 非-1则是慢慢的声音越来越小的停止播放
         * @returns 
         */


        stop(path, s) {
          if (s === void 0) {
            s = -1;
          }

          if ((_crd && Utils === void 0 ? (_reportPossibleCrUseOfUtils({
            error: Error()
          }), Utils) : Utils).isNull(path)) return;
          var audio = this.cachedAudioArr.find(e => e.path == path);

          if (audio) {
            if (!audio.audioSource) return;
            if (!audio.audioSource.playing) return;

            if (audio.time_id) {
              clearInterval(audio.time_id);
            }

            var audioSource = audio.audioSource;

            if (s == -1) {
              audioSource == null || audioSource.pause();
            } else {
              audio.t = 0;
              var interval = 1 / s / 10;
              audio.time_id = setInterval(() => {
                if (audioSource) {
                  audio.t += interval;
                  var volume = this.bgVolumeMax - audio.t / s;

                  if (volume <= 0) {
                    clearInterval(audio.time_id);
                    audioSource == null || audioSource.pause();
                  } else {
                    audioSource.volume = volume <= 0 ? 0 : volume;
                  }
                }
              }, interval * 1000);
            }

            audio.playStatus = false;
          }
        }
        /**
         * 加载音效
         * @param loop 是否循环
         */


        audioSourceLoop(loop) {
          var audioSource = null;

          if (loop) {
            //播放单次的时候具柄
            audioSource = this.getAudioSource(null, true, this.bgVolumeMax);
          } else {
            //循环播放的时候具柄
            audioSource = this.getAudioSource(null, false, 1);
          }

          return audioSource;
        }
        /**
         * 得到一个播放音乐的控制类
         * @param clip 音效片段
         * @param loop 是否循环
         * @param volume 音量
         * @returns 播放类
         */


        getAudioSource(clip, loop, volume) {
          if (clip === void 0) {
            clip = null;
          }

          if (loop === void 0) {
            loop = false;
          }

          if (volume === void 0) {
            volume = 1;
          }

          var audioSource = new AudioSource();

          if (clip) {
            audioSource.clip = clip;
          }

          audioSource.loop = loop;
          audioSource.volume = volume;
          return audioSource;
        }

      }) || _class); //全局单例

      _export("audioMgr", audioMgr = AudioManager.ins());

      _cclegacy._RF.pop();

      _crd = false;
    }
  };
});
//# sourceMappingURL=57a376e67d0206c154a9ca733c4b728af839dc9f.js.map