天天看點

[CocosCreator]封裝音頻管理器

        歡迎喜歡或者從事CocosCreator開發的小夥伴請加入我的大家庭CocosCreator遊戲開發Q群:26855530

import GameKey from "../common/GameKey";
import ConstantSys from "../common/ConstantSys";

/**
 * 音頻管理器
 */
class SoundManager {
    effectPath: string = "sounds/effect/";
    musicPath: string = "sounds/music/";
    sounds: { [key: number]: any } = {};

    musicEnable: boolean = true;
    effectEnable: boolean = true;

    music: string = "";//目前播放的背景音樂

    /**
     * 初始化
     */
    init() {
        cc.audioEngine.setMusicVolume(1);//設定背景音樂的音量 0~1

        cc.resources.loadDir(this.musicPath, cc.AudioClip, (err, clips: cc.AudioClip[]) => {
            if (err) {
                cc.error(err);
                return;
            }
            for (let i = 0; i < clips.length; i++) {
                this.addSound(clips[i].name, clips[i]);
            }
        });

        cc.resources.loadDir(this.effectPath, cc.AudioClip, (err, clips: cc.AudioClip[]) => {
            if (err) {
                cc.error(err);
                return;
            }
            for (let i = 0; i < clips.length; i++) {
                this.addSound(clips[i].name, clips[i]);
            }
        });

        let keyGameMusic: string = cc.sys.localStorage.getItem(GameKey.Key_Game_Music);
        this.musicEnable = ConstantSys.No != keyGameMusic;

        let keyGameEffect: string = cc.sys.localStorage.getItem(GameKey.Key_Game_Effect);
        this.effectEnable = ConstantSys.No != keyGameEffect;

        cc.log("SoundManager 初始化完成");
    }

    addSound(key: string, clip: cc.AudioClip) {
        this.sounds[key] = clip;
    }

    /**
     * 播放音效
     * @param fxName
     * @param loop
     */
    playEffect(fxName: string, loop?: boolean): number {
        if (!this.effectEnable) return -1;
        if (this.sounds[fxName]) {
            return cc.audioEngine.playEffect(this.sounds[fxName], loop);
        } else {
            cc.resources.load(this.effectPath + fxName, cc.AudioClip, (err, clips: cc.AudioClip) => {
                if (err) {
                    cc.error(err);
                    return;
                }
                this.addSound(clips.name, clips);
                return cc.audioEngine.playEffect(clips, loop);
            });
        }
    }

    /**
     * 停播音效
     * @param audioID
     */
    stopEffect(audioID: number) {
        // SysLog.debug("停止音效" + audioID);
        cc.audioEngine.stopEffect(audioID);
    }

    /**
     * 播放音樂
     * @param musicName
     */
    playMusic(musicName: string) {
        if (this.music == musicName) {
            return;
        }
        this.music = musicName;
        if (!this.musicEnable) return;

        if (this.sounds[musicName]) {
            cc.audioEngine.playMusic(this.sounds[musicName], true);
        } else {
            cc.resources.load(this.musicPath + musicName, cc.AudioClip, (err, clips: cc.AudioClip) => {
                if (err) {
                    cc.error(err);
                    return;
                }
                this.addSound(clips.name, clips);
                cc.audioEngine.playMusic(clips, true);
            });
        }
    }

    /**
     * 停播音樂
     */
    stopMusic() {
        cc.audioEngine.stopMusic();
    }

    /**
     * 開關音樂
     * @param enabled
     */
    setMusicEnabled(enabled: boolean) {
        this.musicEnable = enabled;
        if (this.musicEnable) {
            let nowMusic: string = this.music;
            this.music = "";
            this.playMusic(nowMusic);
        } else {
            cc.audioEngine.stopAll();
        }
        cc.sys.localStorage.setItem(GameKey.Key_Game_Music, this.musicEnable ? ConstantSys.Yes : ConstantSys.No);
    }

    getMusicEnable() {
        return this.musicEnable;
    }

    /**
     * 開關音效
     * @param enabled
     */
    setEffectEnabled(enabled: boolean) {
        this.effectEnable = enabled;
        cc.sys.localStorage.setItem(GameKey.Key_Game_Effect, this.effectEnable ? ConstantSys.Yes : ConstantSys.No);
    }

    getEffectEnable() {
        return this.effectEnable;
    }
}

export default new SoundManager();
           

貌似都很簡單,拿來即用,就不多唧唧了~