3aea16ca5e883ea6a52a58693e0c9b740c927d93.js 4.6 KB

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