import { _decorator,AudioSource,AudioClip } from 'cc'; import { GameMng } from '../GameMng'; import { userData } from '../UserData/userData'; import GameData from './GameData'; const { ccclass, property } = _decorator; @ccclass('AudioMng') export default class AudioMng { private static _audioSource?: AudioSource; private audioSource: AudioSource | null = null; private bgmClip: AudioClip | null = null; private static _instance: AudioMng | null = null; public static get Instance() { if (AudioMng._instance === null){ // var s = JSON.stringify({"id":"123123123","name":"xiaoming","room":{"time":"3.1415926"}}) // var obj = JSON.parse(s) // var d = obj as userData // console.log("id == ",d.room) AudioMng._instance = new AudioMng(); } return AudioMng._instance; } public InitSaveData() { GameData.SetCustomData("BgmOn", 1); GameData.SetCustomData("SoundOn", 1); GameData.SetCustomData("Volume", 1); } public get bgmOn() { return GameData.GetCustomData("BgmOn") == 1; } public get soundOn() { return GameData.GetCustomData("SoundOn") == 1; } public get volume() { return GameData.GetCustomData("Volume"); } public PlayBGM(bgm:AudioClip) { if (this.audioSource == null) this.audioSource = new AudioSource(); // if (!this.bgmOn) return; // this.StopBGM(); // this.bgmClip = null; // this.bgmClip = bgm; // this.audioSource.clip = this.bgmClip; // this.audioSource.loop = true; // this.audioSource.volume = this.volume; // this.audioSource.play(); // console.log(this.bgmClip) } public StopBGM(): void { if (this.audioSource) { // this.audioSource.stop(); } } public PauseBGM(): void { if (this.audioSource) { // this.audioSource.pause(); } } /** * 音效 * @param name */ public PlaySoundByName(clip: AudioClip) { if (!this.soundOn) return; // var eid = audioEngine.playEffect(clip, false); // audioEngine.setVolume(eid, 1); if(clip&&this.audioSource){ this.audioSource.playOneShot(clip,1) } } public SetVolume(num: number): void { if (this.audioSource) { this.audioSource.volume = num; GameData.SetCustomData("Volume", num); } } }