import { _decorator, AudioClip, AudioSource, Component, Node, tween } from 'cc'; import { ResourceUtil } from './ResourceUtil'; import { config } from '../config'; import { GameManager } from '../GameManager'; const { ccclass, property } = _decorator; @ccclass('audioManager') export class audioManager extends Component { public arrSound: any = []; public audios_map: Map = new Map; public musicVolume: number = 0.8; public soundVolume: number = 1; private static instance:audioManager =null; public static Instance(){ return this.instance } protected start(): void { audioManager.instance = this this.init() } protected onLoad(): void { let setting_data = GameManager.getSettingData() if(setting_data.isOpenYinYue==false) { return } this.scheduleOnce(()=>{ this.playMusic(config.AUDIO.home_bgm) },3) } public init(){ this.audios_map.clear() for (const key in config.AUDIO) { if (Object.prototype.hasOwnProperty.call( config.AUDIO, key)) { const path = config.AUDIO[key]; ResourceUtil.loadRes(path, AudioClip, (err: any, clip: any)=> { if(!err){ let node = new Node node.parent = this.node node.addComponent(AudioSource).clip = clip this.audios_map.set(path,node) } }); } } } public stopAllBGM(){ if(this.audios_map.get(config.AUDIO.game_bgm)!=null){ this.audios_map.get(config.AUDIO.game_bgm).getComponent(AudioSource).stop() } if(this.audios_map.get(config.AUDIO.home_bgm)!=null){ this.audios_map.get(config.AUDIO.home_bgm).getComponent(AudioSource).stop() } } public playBGM(clip_node:Node){ this.stopAllBGM() clip_node.getComponent(AudioSource).loop = true clip_node.getComponent(AudioSource).play() } public playMusic(path:string){ if(this.audios_map.get(path)!=null){ this.playBGM(this.audios_map.get(path)) }else{ ResourceUtil.loadRes(path, AudioClip, (err: any, clip: any)=> { let node = new Node node.parent = this.node node.addComponent(AudioSource).clip = clip this.audios_map.set(path,node) this.playBGM(node) }); } } public playSound(path:string,soundVolume:number = null){ if(!GameManager.getSettingData().isOpenYinXiao) {return} if(path.length<=0){ return } let loop:boolean = false let play = (node:Node)=>{ node.getComponent(AudioSource).stop() node.getComponent(AudioSource).playOneShot(node.getComponent(AudioSource).clip,1) // node.getComponent(AudioSource).playOneShot(node.getComponent(AudioSource).clip,soundVolume?soundVolume:this.soundVolume) } if(this.audios_map.get(path)){ play(this.audios_map.get(path)) } ResourceUtil.loadRes(path, AudioClip, (err: any, clip: AudioClip)=> { let node = new Node node.parent = this.node node.addComponent(AudioSource).clip = clip this.audios_map.set(path,node) play(node) }); } getSound(){ if(this.node.getComponent(AudioSource)!=null){ return this.node.getComponent(AudioSource) } return this.node.addComponent(AudioSource) } public playHomeBgm() { if(this.getSound().clip==null){ this.playMusic(config.AUDIO.home_bgm) } else { this.getSound().loop = true; this.getSound().play() } } public pauseHomeBgm() { // this.getSound().stop() this.stopAllBGM() } }