[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();

貌似都很简单,拿来即用,就不多唧唧了~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值