import { _decorator, AudioClip, AudioSource, Component, director, Label, Node, resources } from 'cc'; import { EventManager } from '../EventManager'; import HTTPS, { NetPost } from '../MyFrame/HTTPS'; const { ccclass, property } = _decorator; @ccclass('AudioManager') export class AudioManager extends Component { public static BGM = "Sound/gbg";// public static dead = "Sound/dead";// public static anniu = "Sound/anniu";// public static heart = "Sound/heart";// static _Instance: AudioManager; curAudioSE: AudioSource; curAudioBGM: AudioSource; static get Instance(): AudioManager { if (this._Instance == null) { let node = new Node(); let comp = node.addComponent(AudioManager); node.name = "AudioManager"; //加入场景节点 let scene = director.getScene(); scene.addChild(node); director.addPersistRootNode(node); //播放音效的组件 comp.curAudioSE = comp.addComponent(AudioSource); comp.curAudioSE.loop = false; //播放BGM的组件 comp.curAudioBGM = comp.addComponent(AudioSource); comp.curAudioBGM.loop = true; this._Instance = comp; } return this._Instance; } playBgm(name: string) { (async () => { let au = await this.getSoundUrl(name); if (au) { if (localStorage.getItem('Music') == 'true') { this.curAudioBGM.volume = 1 } else { this.curAudioBGM.volume = 0 } this.curAudioBGM.clip = au this.curAudioBGM.play(); } })(); } setBgmvolume() { if (localStorage.getItem('Music') == 'true') { this.curAudioBGM.volume = 1 } else { this.curAudioBGM.volume = 0 } } playEffict(name: string) { (async () => { let au = await this.getSoundUrl(name); if (au) { let temp = 0 if (localStorage.getItem('Effect') == 'true') { temp = 1 } else { temp = 0 } this.curAudioSE.playOneShot(au, temp); } })(); } public getSoundUrl(path: string): Promise { return new Promise((re, rj) => { resources.load(path, AudioClip, (error: Error, resource: AudioClip) => { if (!error) { re(resource); } else { re(null); } }) }) //return cc.url.raw(path) } }