cocos-creator使用记录18_背景音乐的切换

1.cocos中背景音乐的特点
背景音乐通常是循环播放的。
背景音乐由独立于场景的cc.audioEngine控制播放。因此,切场景并不会停止背景音乐。
当有多个背景音乐、或需要开启关闭背景音乐时,需要用全局变量记录背景音乐的Id。
注意:在creator1.10后,引用声音文件要使用完整写法,且要替换url为type,如下
    btnAudio: {default: null, type: cc.AudioClip}


2.压缩声音文件
可使用格式工厂来转换声音文件格式和大小。通常我们使用MP3格式。
有时候,选择低质量生成的声音文件在手机上播放不了,需要根据情况调整生成的质量。


3.代码
Common.js-------------------
module.exports = {
bgmId: null, //背景音乐id
    passBgmId: null, //成功界面背景音乐id
};
Start.js--------------------
var common = require('zqddn_zhb_Common');
cc.Class({
    extends: cc.Component,
    properties: {
soundOnBtn: cc.Node, //声音开按钮
        soundOffBtn: cc.Node, //声音关按钮
btnAudio: {default: null, url: cc.AudioClip}, //按钮音效
        bgmAudio: {default: null, url: cc.AudioClip}, //背景音乐
},
    onLoad: function(){
//声音
        this.isSoundOn = cc.sys.localStorage.getItem('zqddn_zhb_soundOn') || 1;
        if(this.isSoundOn == 1){
            this.playBgm();
            this.soundOnBtn.active = true;
            this.soundOffBtn.active = false;
        }else{
            this.soundOnBtn.active = false;
            this.soundOffBtn.active = true;
        }
},
    playBgm: function(){ //背景音乐
        if(common.bgmId == null){
            common.bgmId = cc.audioEngine.play(this.bgmAudio, true, 1); //第一次播放
        }else{
            var state = cc.audioEngine.getState(common.bgmId); //-1error,0initalzing,1playing,2paused
            if(state == 2){ //暂停状态
                cc.audioEngine.resume(common.bgmId); //恢复背景音乐
            }
        }
    },
    playBtnSound: function () { //按钮音效
        if(this.isSoundOn == 1){
            cc.audioEngine.playEffect(this.btnAudio, false);
        }
    },
});
Success.js--------------------
var common = require('zqddn_zhb_Common');
cc.Class({
    extends: cc.Component,
    properties: {
btnAudio: {default: null, url: cc.AudioClip}, //按钮音效
        bgmAudio: {default: null, url: cc.AudioClip}, //背景音乐
        passBgmAudio: {default: null, url: cc.AudioClip}, //过关背景音乐
    },
onLoad: function() {
        this.isSoundOn = cc.sys.localStorage.getItem('zqddn_zhb_soundOn') || 1;
    },
onEnable: function() { //成功界面是游戏界面中的一个节点
        //声音
        if(this.isSoundOn == 1){
            console.log("0 bgmId=" + common.bgmId);
            cc.audioEngine.pause(common.bgmId); //暂停背景音乐
            console.log("0 passBgmId=" + common.passBgmId);
            if(common.passBgmId == null){
                common.passBgmId = cc.audioEngine.play(this.passBgmAudio, true, 1); //第一次播放过关背景音乐
                console.log("1 passBgmId=" + common.passBgmId);
            }else{
                var state = cc.audioEngine.getState(common.passBgmId); //-1error,0initalzing,1playing,2paused
                console.log("2 passBgmId.state=" + state);
                if(state == 2){ //暂停状态
                    cc.audioEngine.resume(common.passBgmId); //恢复过关背景音乐
                }
            }
        }
},
    resumeBgm: function(){
        if(this.isSoundOn == 1){
            console.log("3 passBgmId=" + common.passBgmId);
            cc.audioEngine.pause(common.passBgmId); //暂停过关背景音乐
            cc.audioEngine.resume(common.bgmId); //恢复背景音乐
        }
    },
    playBtnSound: function () {
        if(this.isSoundOn == 1){
            cc.audioEngine.playEffect(this.btnAudio, false);
        }
    },
onNextBtn: function() { //下一关按钮
        this.playBtnSound();
        this.resumeBgm();
        this.node.active = false;
        common.currentLevel++;
        cc.game.emit("game_initLevel"); //通知游戏界面初始化关卡
    },
});



猜你喜欢

转载自blog.csdn.net/haibo19981/article/details/80452561