cc.Class({
    extends: cc.Component,

    properties: {

        audioSource: {
            type: cc.AudioSource,
            default: []
        },nanaudio:{
            url: cc.AudioClip,
            default: []
        },nvaudio:{
            url: cc.AudioClip,
            default: []
        }
       
    },

    onLoad: function () {
        this._YingLiang = 1;
        this._YingXiao = 1;
        cc.SY = this;
        var YingLiang = parseFloat( cc.sys.localStorage.getItem('yingyue'));
        var YingXiao  =  parseFloat(  cc.sys.localStorage.getItem('yingxiao'));

        if(cc.sys.localStorage.getItem('yingxiao') == null){

            YingXiao = 1;
        }

        if(cc.sys.localStorage.getItem('yingyue') == null){
        
            YingLiang = 1;
        }

        this._YingLiang = YingLiang;
        this._YingXiao = YingXiao;

    },zyconfig(data){

        if(data.audioSource){

            this.audioSource = [];
            for(var i = 0; i < data.audioSource.length ; i++){
                var  shuju  = cc.find(data.audioSource[i]);
                if(shuju){
                    var ziyuan = shuju.getComponent(cc.AudioSource);
                    if(ziyuan){
                        this.audioSource.push( ziyuan );
                    }
                }
            }
        }

        if(data.nanaudio){
            
            this.nanaudio = [];

            for(var i = 0; i < data.nanaudio.length ; i++){

                this.nanaudio.push( cc.url.raw("resources/"+data.nanaudio[i]+".mp3" ));
            }
        }

        if(data.nvaudio){

            this.nvaudio = [];
            for(var i = 0; i < data.nvaudio.length ; i++){
                this.nvaudio.push( cc.url.raw("resources/"+data.nvaudio[i]+".mp3" ));
            }
        }

    },yingyuep(id){

        if (!this.audioSource||  ! this.audioSource[id] ) return;
        this.audioSource[id].volume = this._YingLiang ;
        this.audioSource[id].play();

    },yingyuet(id){

        if (!this.audioSource||  ! this.audioSource[id] ) return;
        this.audioSource[id].stop();

    },yingyuez(id){

        if (!this.audioSource||  ! this.audioSource[id] ) return;
        this.audioSource[id].pause();

    },yingyuec(id){

        if (!this.audioSource||  ! this.audioSource[id] ) return;
        this.audioSource[id].resume();

    },yingyueallstop(){
        
        if (!this.audioSource || this._YingLiang <= 0 ) return;

        for(var i = 0; i <this.audioSource.length;i++){

           
            this.audioSource[i].stop();
        }
      
      
      

    },yingyueallsetnum(){

        if (!this.audioSource) return;

        for(var i = 0; i <this.audioSource.length;i++){

            this.audioSource[i].volume = this._YingLiang*1 ;
        }


    },yingxiaop(id,sex){

        if(this._YingXiao <= 0){

            return ;
        }

        if(sex == '0'){

            if (!this.nvaudio||  ! this.nvaudio[id] ) return;
            cc.audioEngine.play(this.nvaudio[id], false, (this._YingXiao *1 +0.2));

        }else{
   
        
            if (!this.nanaudio||  ! this.nanaudio[id] ) return;
            cc.audioEngine.play(this.nanaudio[id], false,(this._YingXiao*1+0.2));
        }



    },yingxiaopurl( url ){

        var lujin = cc.url.raw("resources/"+url+".mp3" );
        
        if(lujin){
            cc.audioEngine.play(lujin, false,(this._YingXiao*1+0.2));
        }

    },yingyuepurl( url ){

        var finddds = "shengying"+ (url.replace(/\//g,"G"));
        var shengnode = cc.find("Canvas/"+finddds);
        var lujin = cc.url.raw("resources/"+url+".mp3" );
        if(!shengnode){
            shengnode = new  cc.Node(finddds);
            var yingliang = shengnode.addComponent(cc.AudioSource);
            


            yingliang.volume = 1;
            yingliang.clip = lujin;
            yingliang.loop = true;
            yingliang.mute = false;
            yingliang.playOnLoad= false;
            yingliang.preload= false;
            this.audioSource.push( yingliang );
            shengnode.parent = cc.find("Canvas");
            
            
            

        }else{

            var yingliang =  shengnode.getComponent( cc.AudioSource );
        }

       

        if(yingliang){

            yingliang.volume = this._YingLiang ;
            yingliang.play();
            
        }

        

    },yingxiaot(){

        /*所有音效停止*/
        cc.audioEngine.stopAll();

    },yingxiaoz(){

        /*所有音效暂停*/

        cc.audioEngine.pauseAll();

    },yingxiaoc(){
        /*所有音效重放*/
        cc.audioEngine.resumeAll();

    },setyingxiao(num){

        num = parseFloat(num);

        if(num > 1){
            num = 1;
        }else if(num <=0){
             num = 0.0;
        }

        this._YingXiao = num;
        cc.sys.localStorage.setItem('yingxiao',num);

    },setyingyue(num){

        num = parseFloat(num);
        
        if(num > 1){
            num = 1;
        }else if(num <=0){
             num = 0.0;
             
        }

        this._YingLiang = num;
        this.yingyueallsetnum();
        cc.sys.localStorage.setItem('yingyue',num);

    },duqu(){

        return [this._YingLiang,this._YingXiao]; 
    }
    



});
