cc.Class({
    extends: cc.Component,

    properties: {

    },
    onLoad()
    {
        this.musicNameArr = [];
        this.musicArr = [];
    },
    play(name)
    {
        for(let i=0;i<this.musicNameArr.length;i++)
        {
            if(name == this.musicNameArr[i])
            {
                return;
            }
        }

        let self = this;

        cc.loader.loadRes(name, cc.AudioClip, (err, audioClip)=> {
            let aAudio = cc.audioEngine.play(audioClip, false, 1);
            self.musicNameArr.push(name); 
            self.musicArr.push(aAudio); 
        });
    },
    playAudio(name,callback)
    {
        for(let i=0;i<this.musicNameArr.length;i++)
        {
            if(name == this.musicNameArr[i])
            {
                return;
            }
        }

        let self = this;

        cc.loader.loadRes(name, cc.AudioClip, (err, audioClip)=> {
            let aAudio = cc.audioEngine.play(audioClip, false, 1);
            self.musicNameArr.push(name); 
            self.musicArr.push(aAudio); 
            cc.audioEngine.setFinishCallback(aAudio, function ()
            {
                self.stop(name);
                callback();
            });
        });
    },
    playBGM(name,volume,callback)
    {
        for(let i=0;i<this.musicNameArr.length;i++)
        {
            if(name == this.musicNameArr[i])
            {
                return;
            }
        }
        let self = this;

        cc.loader.loadRes(name, cc.AudioClip, (err, audioClip)=> {
            
            let aAudio = cc.audioEngine.play(audioClip, true, volume);
            self.musicNameArr.push(name); 
            self.musicArr.push(aAudio); 
            callback();
        });
    },
    stop(name)
    {
        for(let i=0;i<this.musicNameArr.length;i++)
        {
            if(name == this.musicNameArr[i])
            {
                cc.audioEngine.stop(this.musicArr[i]);

                this.musicArr.splice(i,1);
                this.musicNameArr.splice(i,1);
                return;
            }
        }
    },
    stopAll()
    {
        for(let i=0;i<this.musicNameArr.length;i++)
        {
            cc.audioEngine.stop(this.musicArr[i]); 
        }

        this.musicArr.length = 0;
        this.musicNameArr.length = 0;
    }
});
