1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import { _decorator, AudioClip, AudioSource, Component, Node } from 'cc';
- import { ResourceUtil } from './ResourceUtil';
- const { ccclass, property } = _decorator;
- @ccclass('audioManager')
- export class audioManager extends Component {
- public arrSound: any = [];
- public audios: any = {};
- public musicVolume: number = 0.8;
- public soundVolume: number = 1;
- public playMusic(clip:AudioClip){
- if(clip!=null){
- this.getSound().clip = null;
- this.getSound().loop = true;
- this.getSound().clip = clip;
- this.getSound().play()
- }
- }
- public playSound(path:string){
- if(path.length<=0){
- return
- }
- let loop:boolean = false
- ResourceUtil.loadRes(path, AudioClip, (err: any, clip: any)=> {
- let tmp = {} as any;
- tmp.clip = clip;
- tmp.loop = loop;
- tmp.isMusic = false;
- this.arrSound.push(tmp);
- if (loop) {
- this.audios[path] = tmp;
- clip.setLoop(loop);
- }
- clip.setVolume(this.soundVolume);
-
- clip.playOneShot();
- clip.once('ended', ()=>{
- ResourceUtil.remove(this.arrSound, (obj: any)=>{
- return obj.clip === tmp.clip;
- });
- })
- });
- }
- getSound(){
- if(this.node.getComponent(AudioSource)!=null){
- return this.node.getComponent(AudioSource)
- }
- return this.node.addComponent(AudioSource)
- }
- }
|