// import LocalData from "../Template/LocalData"; import LocalData from "./LocalData"; export default class AudioManager { public static readonly instance: AudioManager = new AudioManager(); public static HALL_BG_MUSIC = "res/Audio/通用bgm"; public static Game_BG_MUSIC = "res/Audio/通用bgm"; public static BUTTON_EFFECT1 = "res/Audio/btn1"; public static eatJinbi = "res/Audio/吃金币音效"; public static 预发射音效 = "res/Audio/预发射音效"; public bgPath = null; public playOnlyOnceAudioRecord: object = null; constructor() { this.UpdateVolume() } public UpdateVolume() { if (LocalData.getInstance().getMusic()) { cc.audioEngine.setMusicVolume(1) } else { cc.audioEngine.setMusicVolume(0) } if (LocalData.getInstance().getEffects()) { cc.audioEngine.setEffectsVolume(1) } else { cc.audioEngine.setEffectsVolume(0) } } public getSoundUrl(path: string): Promise { return new Promise((re, rj) => { // cc.loader.loadRes(path, cc.AudioClip, (error: Error, resource: cc.AudioClip) => { // if (!error) { // re(resource); // } else { // re(null); // } // }); let bundle = cc.assetManager.getBundle("sub"); bundle.load(path, cc.AudioClip, (error: Error, resource: cc.AudioClip) => { if (!error) { re(resource); } else { re(null); } }); }) } public playBg(path: string) { cc.audioEngine.stopMusic(); (async () => { let au = await this.getSoundUrl(path); if (cc.isValid(au)) { let audioId = cc.audioEngine.playMusic(au, true); } })(); } public playEffect(effectName: string) { (async () => { let au = await this.getSoundUrl(effectName); if (cc.isValid(au)) { let audioId = cc.audioEngine.playEffect(au, false); } })(); } public playGameBg() { this.playBg(AudioManager.Game_BG_MUSIC); } public playHallBg() { this.playBg(AudioManager.HALL_BG_MUSIC); } public playBtnEffect(effectType: number = 0) { switch (effectType) { case 0: this.playEffect(AudioManager.BUTTON_EFFECT1); break; case 1: break; case 2: break; default: break; } } public static stopBg() { cc.audioEngine.stopMusic(); } }