import { AudioClip, AudioSource, Node, Component, _decorator } from "cc"; import SoundNodePool from "./SoundNodePool"; import { EGlobalEvent } from "../MesEvent/EGlobalEvent"; import MesMgr from "../MesEvent/MesMgr"; /**音频组件代理 */ const {ccclass, property,menu} = _decorator; @ccclass("AudioEx") @menu("扩展/音频扩展") export default class AudioEx extends AudioSource { isRemove:boolean = false; init(isRemove: boolean, Parent: Node) { this.isRemove = isRemove; this.node.setParent(Parent); if (this.isRemove) { this.loop = false; MesMgr.on(EGlobalEvent.停止所有音效, this.stop, this); } else { this.loop = true; } } setVolume(volume:number){ this.volume = volume; } playOne(audio: AudioClip, callback?: Function) { this.clip = audio; if(this.currentTime!=0){this.currentTime = 0;} this.play(); this.isRemove && this.scheduleOnce(() => { this.stop(); callback && callback(); }, audio.getDuration()); } playloop(audio: AudioClip) { this.stop(); this.clip = audio; this.play(); } stop(){ if (!this.clip) { return; } this.isRemove && this.unscheduleAllCallbacks(); super.stop(); this.clip = null; this.isRemove && SoundNodePool.pushPoxy(this); } isPlaying(){ return !!this.clip; } onDestroy(): void { MesMgr.offAllCaller(this); } }