123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import { Component, game, Node } from "cc";
- import { storage } from "../storage/SqlUtil";
- import { AudioEffect } from "./AudioEffect";
- import { AudioMusic } from "./AudioMusic";
- export interface AudioConf {
- bundle?: string;
- 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;
- }
-
- 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;
- }
- }
-
- 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);
- }
- }
|