import { _decorator, assetManager, AudioClip, AudioSource, Component, Node } from 'cc';
import { gameManager } from './gameManager';
const { ccclass, property } = _decorator;

@ccclass('sysSound')
export class sysSound extends Component {
    public playSound(path:string){
        if(!gameManager.getUserData().isOpenYinXiao){
            return
        }
        if(path.length<=0){
            return
        }
        let call_back = (err: any, clip: AudioClip)=> {
            if(!err){
                gameManager.mp3_cache.set(path,clip)
                this.playClip(clip); 
            }
        }
        if(gameManager.mp3_cache.get(path)){
            let clip = gameManager.mp3_cache.get(path);
            call_back(null,clip)
        }else{
            assetManager.loadRemote(path, call_back );
        }
    }
    public playClip (clip: AudioClip) {
        this.getSound().stop()
        this.getSound().loop = false;
        this.getSound().clip = clip;
        this.getSound().play()
    }
    getSound(){
        if(this.node.getComponent(AudioSource)!=null){
            return this.node.getComponent(AudioSource)
        }
        return this.node.addComponent(AudioSource)
    }
}


