SoundEffects.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. Object.defineProperty(i, "__esModule", {value: !0}), (i.AUDIO_DIR = void 0);
  5. t = t("BaseSound");
  6. (i.AUDIO_DIR = "audio/"),
  7. (i.default = class extends t.default {
  8. constructor() {
  9. super(), (this._loopSound = new Map());
  10. }
  11. play(t) {
  12. t = this.getSound(t);
  13. return t ? this.playSound(t) : null;
  14. }
  15. stopLoopEffect(t) {
  16. let e = this._loopSound.get(t);
  17. e &&
  18. (e.cnt--,
  19. e.cnt <= 0 &&
  20. (e.channel && (cc.audioEngine.stopEffect(e.channel), (e.channel = null)),
  21. this._loopSound.delete(t)));
  22. }
  23. loopPlay(t) {
  24. var e;
  25. return (
  26. this._loopSound.has(t) ||
  27. ((e = this.getLoopSound(t))
  28. ? ((e = cc.audioEngine.playEffect(e, !0)), this._loopSound.set(t, {cnt: 1, channel: e}))
  29. : this._loopSound.set(t, {cnt: 1, channel: null})),
  30. t
  31. );
  32. }
  33. getLoopSound(t) {
  34. var e = i.AUDIO_DIR + t,
  35. t = cc.resources.get(e, cc.AudioClip);
  36. return t || cc.resources.load(e, cc.AudioClip, this.onLoopResourceLoadComplete.bind(this)), t;
  37. }
  38. onLoopResourceLoadComplete(t, e) {
  39. let i = this._loopSound.get(e.name);
  40. i && !isNaN(i.channel) && (i.channel = cc.audioEngine.playEffect(e, !0));
  41. }
  42. playSound(t) {
  43. if (t) return cc.audioEngine.playEffect(t, !1);
  44. }
  45. setVolume(t) {
  46. (this._volume = t), cc.audioEngine.setEffectsVolume(t);
  47. }
  48. loadedPlay(t) {
  49. this.playSound(t);
  50. }
  51. pauseEffect() {
  52. cc.audioEngine.pauseAllEffects();
  53. }
  54. resumeEffect() {
  55. cc.audioEngine.resumeAllEffects();
  56. }
  57. });