import { Component, game, Node } from "cc"; import { storage } from "../storage/SqlUtil"; import { AudioEffect } from "./AudioEffect"; import { AudioMusic } from "./AudioMusic"; export interface AudioConf { bundle?: string; //BundleName url: string; //资源路径 } export class AudioManager extends Component { private _audio_conf: { [key: number]: AudioConf } = {}; private _local_data: any = {}; private _music: AudioMusic = null; private _effect: AudioEffect = null; private _volume_music: number = 1; private _volume_effect: number = 1; private _switch_music: boolean = true; private _switch_effect: boolean = true; private _load_key = "audio_conf"; private static _instance: AudioManager; static get instance() { if (!this._instance) { let node = new Node("Audio_Manager"); game.addPersistRootNode(node); this._instance = node.addComponent(AudioManager); this._instance._init(); let music = new Node("Music_Node"); node.addChild(music); this._instance._music = music.addComponent(AudioMusic); let effect = new Node("Effect_Node"); node.addChild(effect); this._instance._effect = effect.addComponent(AudioEffect); } return this._instance; } private _init() { let data = storage.get(this._load_key); if (data) { try { this._local_data = JSON.parse(data); this._volume_music = this._local_data.volume_music; this._volume_effect = this._local_data.volume_effect; this._switch_music = this._local_data.switch_music; this._switch_effect = this._local_data.switch_effect; } catch (e) { this._local_data = {}; this._volume_music = 1; this._volume_effect = 1; this._switch_music = true; this._switch_effect = true; } this._music.volume = this._volume_music; this._effect.volume = this._volume_effect; } } init(conf: any) { this._audio_conf = conf; } /** * 播放背景音乐 * @param id 资源ID * @param callback 音乐播放完成事件 */ playMusic(id: number, callback: Function = null,loop: boolean = true) { let conf = this._audio_conf[id]; if (conf && this._switch_music) { if(!this._music){ this._music=new AudioMusic() } this._music.load(conf.bundle, conf.url,null,loop); this._music.onComplete = callback; } } /** * 播放音效 * @param id 资源ID */ playEffect(id: number) { let conf = this._audio_conf[id]; if (conf && this._switch_effect) { if(!this._effect){ this._effect=new AudioEffect() } this._effect.load(conf.bundle, conf.url); } } //背景音乐音量 get musicVolume() { return this._volume_music; } set musicVolume(value: number) { this._volume_music = value; this._music.volume = value; } //音效音量 get effectVolume() { return this._volume_effect; } set effectVolume(value: number) { this._volume_effect = value; this._effect.volume = value; } //音乐开关 getSwitchMusic() { return this._switch_music; } setSwitchMusic(value: boolean) { this._switch_music = value; (!value) && this.stopMusic(); } //音效开关 getSwitchEffect() { return this._switch_effect; } setSwitchEffect(value: boolean) { this._switch_effect = value; (!value) && this._effect.stop(); } resumeMusic() { (this._music) && this._music.play(); } pauseMusic() { (this._music) && this._music.pause(); } stopMusic() { (this._music) && this._music.stop(); } save() { this._local_data.volume_music = this._volume_music; this._local_data.volume_effect = this._volume_effect; this._local_data.switch_music = this._switch_music; this._local_data.switch_effect = this._switch_effect; let data = JSON.stringify(this._local_data); storage.set(this._load_key, data); } }