123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- import { SaveUtil } from "../help/SaveUtil";
- import ResMgr from "./ResMgr";
- const { ccclass, property } = cc._decorator;
- var MUSIC_NUM = 2;
- var EFFECT_NUM = 8;
- @ccclass
- export default class SoundMgr extends cc.Component {
- public static Instance: SoundMgr = null;
- private effect_volume: number = 1;
- private music_volume: number = 0.9;
- private music_as: Array<cc.AudioSource> = [];
- private effect_as: Array<cc.AudioSource> = [];
- private curEff_as: number = 0;
- onLoad() {
- if (SoundMgr.Instance === null) {
- SoundMgr.Instance = this;
- }
- else {
- this.destroy();
- return;
- }
- // this.music_as = this.node.addComponent(cc.AudioSource);
- // this.music_as.volume = this.music_volume;
- this.MusicMute = SaveUtil.get("music_mute", "0") == "1";//默认不禁用
- // if (this.MusicMute) {
- // this.music_as.volume = 0;
- // }
-
- this.EffMute = SaveUtil.get("effect_mute", "0") == "1";//默认不禁用
- // for(var i = 0; i < EFFECT_NUM; i ++) {
- // var as = this.node.addComponent(cc.AudioSource);
- // this.effect_as.push(as);
- // as.volume = this.effect_volume;
- // if (this.EffMute) {
- // as.volume = 0;
- // }
- // }
- // this.music_volume = 0.05;
- this.initAudio(EFFECT_NUM,this.effect_as,this.effect_volume,this.EffMute);
- this.initAudio(MUSIC_NUM,this.music_as,this.music_volume,this.MusicMute);
- this.curEff_as = 0;
- }
- private initAudio(num:number,arr:Array<cc.AudioSource>,volume:number,mute:boolean ){
- for(var i = 0; i < num; i ++) {
- var as = this.node.addComponent(cc.AudioSource);
- arr.push(as);
- as.volume = volume;
- if (mute) {
- as.volume = 0;
- }
- }
- }
- get_music_volume() {
- return this.music_volume;
- }
- set_music_volume(value) {
- for(var i = 0; i < this.music_as.length; i ++) {
- this.music_as[i].volume = value;
- }
- this.music_volume = value;
- cc.sys.localStorage.setItem("music_volume", value);
- }
- private musicMute:boolean = true;
- get MusicMute():boolean{
- return this.musicMute;
- }
- set MusicMute(b_mute:boolean) {
- if (this.musicMute == b_mute) {
- return;
- }
- this.musicMute = b_mute;
- for(var i = 0; i < this.music_as.length; i ++) {
- if (b_mute) {
- this.music_as[i].volume = 0;
- }
- else {
- this.music_as[i].volume = this.music_volume;
- }
- }
- cc.sys.localStorage.setItem("music_mute", b_mute?"1":"0");
- }
- ToogleMusicMute(){
- this.MusicMute = !this.MusicMute;
- return this.MusicMute;
- }
- get_effect_volume() {
- return this.effect_volume;
- }
- set_effect_volume(value) {
- for(var i = 0; i < this.effect_as.length; i ++) {
- this.effect_as[i].volume = value;
- }
- this.effect_volume = value;
- cc.sys.localStorage.setItem("effect_volume", value);
- }
- private effMute = false;
- get EffMute():boolean{
- return this.effMute;
- }
- set EffMute(b_mute:boolean) {
- if (this.EffMute == b_mute) {
- return;
- }
- this.effMute = b_mute;
- for(var i = 0; i < this.effect_as.length; i ++) {
- if (b_mute) {
- this.effect_as[i].volume = 0;
- }
- else {
- this.effect_as[i].volume = this.effect_volume;
- }
- }
- cc.sys.localStorage.setItem("effect_mute", b_mute?"1":"0");
- }
-
- ToogleEffectMute(){
- this.EffMute = !this.EffMute;
- return this.EffMute;
- }
- play_main_music(url, loop) {
- loop = (loop) ? true : false;
- var as = this.music_as[0];
- as.loop = loop;
- as.clip = ResMgr.Instance.getAsset("Sounds", url);
- if (as.clip) {
- as.play();
-
- }
- else {
- cc.error("music audio clip null: ", url);
- }
- }
- play_sub_music(url, loop) {
- loop = (loop) ? true : false;
- var as = this.music_as[1];
- as.loop = loop;
- as.clip = ResMgr.Instance.getAsset("Sounds", url);
- if (as.clip) {
- as.play();
- }
- else {
- cc.error("music audio clip null: ", url);
- }
- }
- stop_sub_music(){
- this.music_as[1].stop();
- }
- stop_music() {
- // this.music_as.stop();
- for(var i = 0; i < this.music_as.length; i ++) {
- this.music_as[i].stop();
- }
- }
- play_effect(url) {
- var as = this.effect_as[this.curEff_as];
- this.curEff_as ++;
- if (this.curEff_as >= EFFECT_NUM) {
- this.curEff_as = 0;
- }
- as.clip = ResMgr.Instance.getAsset("Sounds", url);
- if (as.clip) {
- as.play();
- }
- else {
- cc.error("effect audio clip null: ", url);
- }
- }
- playClickBtn(){
-
- this.play_effect("BMButton_click");
- }
- /**
- * 获取音乐开关状态
- */
- getSwitchMusic(){
- return this.EffMute;
- }
- /**
- * 暂停当前播放音乐
- */
- setPauseMusic(){
- cc.audioEngine.pauseMusic();
- }
- /**
- * 恢复当前被暂停音乐音乐
- */
- setResumeMusic(){
- cc.audioEngine.resumeMusic();
- }
- /**
- * 重新播放该背景音乐
- */
- replayMusic(){
- cc.audioEngine.resumeMusic();
- }
- /**
- * 暂停播放音乐
- * releaseData: 控制是否释放音乐资源 true释放资源 | false不释放资源
- */
- setStopMusic(releaseData = true){
- cc.audioEngine.stopMusic();//releaseData
- }
- }
|