cocos creator 音频控制
音频控制脚本
import userData from "userData";
import resPath from "../config/resPath";
cc.Class({
extends: cc.Component,
properties: {
},
//播放背景音乐
playBgSound() {
let soundState = userData.soundState;
if (soundState == 0) {
cc.audioEngine.pauseMusic();
return;
} else {
this.stopSound();
let path = "";
path = "sound/bgSound";
cc.loader.loadRes(path, cc.AudioClip, (err, res) => {
if (err) { console.log(err) };
cc.audioEngine.playMusic(res, true, 1);
});
};
},
//==============================================
//停止所有的背景音乐
stopSound() {
cc.audioEngine.stopMusic();
cc.audioEngine.stopAllEffects();
},
//--------------------------------------------------------
//暂停所有声音
pauseAllSound() {
cc.audioEngine.pauseAllEffects();
cc.audioEngine.pauseMusic();
},
//恢复播放暂停
resumeAllSound() {
let soundState = userData.soundState;
if (soundState == 0) {
return
} else {
cc.audioEngine.resumeMusic();
cc.audioEngine.resumeAllEffects();
};
},
//--------------------------------------------------------
//播放音效
playSoundEffect(name) {
let soundState = userData.soundState;
if (soundState == 0) {
cc.audioEngine.pauseAllEffects();
return
} else {
let path = "sound/" + name;
cc.loader.loadRes(path, cc.AudioClip, (err, res) => {
if (err) { console.log(err) };
cc.audioEngine.playEffect(res, false, 1);
});
};
},
onLoad() {
},
start() {
},
// update (dt) {},
});