AudioManager.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import LocalData from "../Template/LocalData";
  2. export default class AudioManager {
  3. public static readonly instance: AudioManager = new AudioManager();
  4. public static HALL_BG_MUSIC = "res/Audio/hall";
  5. public static Game_BG_MUSIC = "res/Audio/game";
  6. public static BUTTON_EFFECT1 = "res/Audio/btn1";
  7. public static seat_click_start = "res/Audio/seat_anzhu";
  8. public static seat_click_end = "res/Audio/seat_songkai";
  9. public bgPath = null;
  10. public playOnlyOnceAudioRecord: object = null;
  11. constructor() {
  12. this.UpdateVolume()
  13. }
  14. public UpdateVolume() {
  15. if (LocalData.getInstance().getMusic()) {
  16. cc.audioEngine.setMusicVolume(1)
  17. } else {
  18. cc.audioEngine.setMusicVolume(0)
  19. }
  20. if (LocalData.getInstance().getEffects()) {
  21. cc.audioEngine.setEffectsVolume(1)
  22. } else {
  23. cc.audioEngine.setEffectsVolume(0)
  24. }
  25. }
  26. public getSoundUrl(path: string): Promise<cc.AudioClip> {
  27. return new Promise<cc.AudioClip>((re, rj) => {
  28. let bundle = cc.assetManager.getBundle("sub");
  29. // bundle.load(path, (err, Asset: T) => {
  30. bundle.load(path, cc.AudioClip, (error: Error, resource: cc.AudioClip) => {
  31. if (!error) {
  32. re(resource);
  33. } else {
  34. re(null);
  35. }
  36. });
  37. })
  38. }
  39. public playBg(path: string) {
  40. cc.audioEngine.stopMusic();
  41. (async () => {
  42. let au = await this.getSoundUrl(path);
  43. if (cc.isValid(au)) {
  44. let audioId = cc.audioEngine.playMusic(au, true);
  45. }
  46. })();
  47. }
  48. public playEffect(effectName: string) {
  49. (async () => {
  50. let au = await this.getSoundUrl(effectName);
  51. if (cc.isValid(au)) {
  52. let audioId = cc.audioEngine.playEffect(au, false);
  53. }
  54. })();
  55. }
  56. public playGameBg() {
  57. this.playBg(AudioManager.Game_BG_MUSIC);
  58. }
  59. public playHallBg() {
  60. this.playBg(AudioManager.HALL_BG_MUSIC);
  61. }
  62. public playBtnEffect(effectType: number = 0) {
  63. switch (effectType) {
  64. case 0:
  65. this.playEffect(AudioManager.BUTTON_EFFECT1);
  66. break;
  67. case 1:
  68. break;
  69. case 2:
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. public static stopBg() {
  76. cc.audioEngine.stopMusic();
  77. }
  78. }