混合的声音难题

时间:2017-09-25 10:15:36

标签: javascript html mp3

我用w3schools和www上的几个脚本制作了一个记忆游戏。 http://samuel.dumont.pagesperso-orange.fr/essaijs/numbers/auditory.html 当我在我的硬盘上运行脚本时,它运行良好。确实非常好。声音的开始和结束一个接一个地完成其他声音。 一旦我把它放在我的网站空间上就会出现问题。声音几乎全部播放,而控制台中没有显示错误。 我可以帮忙吗?

可以轻松查看来源,但以防万一。

<script language=javascript>

    var a = document.createElement('audio');
    console.log(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));   


    window.onload=makpag;
    var audiostr="0,1,2,3,4,5,6,7,8,9";
    var language = ['jon','juan','gunther'];
    var llang=language.length-1;
    shuffle(language);      

    var seq=new Array();
    var x=0;
    var b=0;
    var r=8;
    var rep=new String();
    var ln=new Number();
    var add=new Number();
    var conv=new Number();
    var err=0;
    var seqstr=new String();
    var delaytab=new Array();   

    function makpag(){
        var html=new Array();
        var selecthtml=new Array();
        var y=0;

        selecthtml[y]="<select id=lnseq>";y++;
        for(var j=1;j<=30;j++,y++){
                    selecthtml[y]="<option>"+j+"</option>";
        }
        selecthtml[y]="</select>";  

        y=0;
        html[y]="<table id=\"layer\" height=\"100%\">";y++;
        html[y]="<tr>";y++;
        html[y]="<td colspan=3>Delay<input type=\"checkbox\" id=\"gap\"> | Reverse<input type=\"checkbox\" id=\"rev\"> | Length "+selecthtml.join("")+"</td>";y++;
        html[y]="</tr>";y++;        


        html[y]="<tr>";y++;
        html[y]="<td id=\"left\"></td><td id=\"center\" valign=\"top\"><input type=\"text\" id=\"check\"><p id=\"act\"><a href=\"javascript:makseq()\">say</a></p><p id=\"state\"></p></td><td id=\"right\"></td>";y++;
        html[y]="</tr>";y++;
        html[y]="</table>";
        document.getElementById("page").innerHTML=html.join("");
        makvum();




    }

    function makvum(){
        var html=new Array();
        var y=0;
        var v=8;
        html[y]="<table id=\"vum\">";y++;
        html[y]="<tr>";y++;
        for(var z=0;z<=7;z++){
            html[y]="<tr>";y++;     
            html[y]="<td>"+v+"</td><td ></td>";y++;
            html[y]="</tr>";y++;
            v--;
        }
        html[y]="</tr>";y++;
        html[y]="</table>";
        document.getElementById("left").innerHTML=html.join("");        
    }

    function maktab(){
        var html=new Array();
        var y=0;
        document.getElementById("act").innerHTML="<a href=\"javascript:check()\">check</a>";
        html[y]="<table id=\"console\">";y++;
        html[y]="<tr>";y++;
        html[y]="<td onClick=put(7)>7</td><td onClick=put(8)>8</td><td onClick=put(9)>9</td>";y++;
        html[y]="</tr>";y++;
        html[y]="<tr>";y++;
        html[y]="<td onClick=put(4)>4</td><td onClick=put(5)>5</td><td onClick=put(6)>6</td>";y++;
        html[y]="</tr>";y++;
        html[y]="<tr>";y++;
        html[y]="<td onClick=put(1)>1</td><td onClick=put(2)>2</td><td onClick=put(3)>3</td>";y++;
        html[y]="</tr>";y++;
        html[y]="<tr>";y++;
        html[y]="<td onClick=rem()><-</td><td onClick=put(0)>0</td><td onClick=sanitol()>X</td>";y++;
        html[y]="</tr>";y++;
        html[y]="</table>";
        document.getElementById("right").innerHTML+=html.join("");
    }   
    //clear() mot reservé JS
    function rem(){
        var wo= document.getElementById("check").value;
        var wol=wo.length;
        wo=wo.substr(0,wol-1);
        document.getElementById("check").value=wo;
    }       


    function sanitol(){
    console.log("clear");
        document.getElementById("check").value="";
    }   

    function put(it){
        document.getElementById("check").value+=it;
    }

    function check(){

        if(document.getElementById("rev").checked==true){
            var revstr=new String();
            var reverseq=new Array();
            reverseq=seq.reverse();
            var str1=reverseq.join("");
            /*
            longueur de seq 
            for à rebours
            seq.reverse();var str1=seq.join("")
            */
        }
        else{var str1=seq.join("");}

        var str2=document.getElementById("check").value;        
        str1=parseInt(str1);
        str2=parseInt(str2);
        console.log("check err="+err+", rev="+document.getElementById("rev").checked+", str1="+str1+", str2="+str2)
        if(str1==str2 || err==1){
        //alert(err)
            var color=new String();
            switch(r){
                case 9:color="#A00000";
                break;
                case 8:color="#980000";
                break;              
                case 7:color="#900000";
                break;              
                case 6:color="#880000";
                break;
                case 5:color="#800000";
                break;              
                case 4:color="#780000";
                break;                      
                case 3:color="#700000";
                break;
                case 2:color="#680000";
                break;              
                case 1:color="#600000";
                break;                          
            }
            document.getElementById("vum").rows[r].cells[1].style.cssText = 'background-color:'+color;
            x=0;

            document.getElementById("act").innerHTML="<a href=\"javascript:makseq()\">say</a>";
            document.getElementById("right").innerHTML="";
            //document.getElementById("cheat").innerHTML="";
            document.getElementById("check").value="";
            if(err==1){document.getElementById("state").innerHTML="<font color=red>Incorrect</font>";err=0;}
            else{
                document.getElementById("state").innerHTML="<font color=green>Correct</font>";
                if(b==llang){b=0;}
                else{b++;}
            }
            if(r==1){
                makvum();
                r=8;
            }
            else{r--;}
        }
        else{
            document.getElementById("state").innerHTML="<font color=red>Incorrect</font>";
            err++;
            x=0;

            document.getElementById("check").value="";
            document.getElementById("right").innerHTML="";
            document.getElementById("act").innerHTML="<a href=\"javascript:convert(seqstr)\">say</a>";
        }
    }

    function makseq(){
        var seq=new Array();
        var tosplit=new Array();
        var cursor=new Number();
        var digit=new Number();
        var tokeep=new Number();
        document.getElementById("state").innerHTML="";
        document.getElementById("act").innerHTML="";

        tosplit=audiostr.split(",");
        ln=tosplit.length-1;//ln=9      
        cursor=Math.round(Math.random()*(ln - 0) + 0);
        digit=tosplit[cursor];
        seq.push(digit);                
        tokeep  =   tosplit[cursor];
        tosplit.splice(cursor,1);
        var sln=document.getElementById("lnseq").value;//la variable qui definit la longueur de la sequence
        sln=parseInt(sln);
        for (var i=0;i<sln-1;i++){              
            ln=tosplit.length-1;            
            cursor=Math.round(Math.random()*(ln - 0) + 0);
            digit=tosplit[cursor];              
            seq.push(digit);
            tosplit.push(tokeep);
            tokeep=tosplit[cursor]; 
            tosplit.splice(cursor,1);
        }
        //document.getElementById("cheat").innerHTML=seq.join(",");
        seqstr=seq.join(",");

        if(document.getElementById("gap").checked){
            var delayseq=new String();//sera pas necessaire
            var io=0;
            for(var j=0;j<sln;j++){
                io=Math.round(Math.random()*(1 - 0) + 0);
                if(io==1){
                    delaytab[j]=Math.round(Math.random()*(2500 - 500) + 500);
                }
                else{
                    delaytab[j]=0;              
                }   
            }
            delayseq=delaytab.join(",");//panecessaire
        }       

        convert(seqstr);
    }

    function convert(str){      


        rep=language[b];

        console.log("rep: "+rep+", b="+b);  

        if(err==1)document.getElementById("state").innerHTML="";
        seq=str.split(",");
        ln=seq.length;
        conv=parseInt(seq[x]);
        parseA(conv);
    }

    function parseA(conv){

        var audio=new Audio();

        switch(conv){
            case 0 : audio = new Audio('voices/'+rep+'/zero.mp3');
            break;
            case 1 : audio = new Audio('voices/'+rep+'/one.mp3');
            break;
            case 2 : audio = new Audio('voices/'+rep+'/two.mp3');
            break;
            case 3 : audio = new Audio('voices/'+rep+'/three.mp3');
            break;
            case 4 : audio = new Audio('voices/'+rep+'/four.mp3');
            break;
            case 5 : audio = new Audio('voices/'+rep+'/five.mp3');
            break;
            case 6 : audio = new Audio('voices/'+rep+'/six.mp3');
            break;
            case 7 : audio = new Audio('voices/'+rep+'/seven.mp3');
            break;
            case 8 : audio = new Audio('voices/'+rep+'/eight.mp3');
            break;
            case 9 : audio = new Audio('voices/'+rep+'/nine.mp3');
            break;
        }   

        //alert("ok")         
        audio.load();
        audio.play();

        audio.addEventListener("loadeddata", myFunction);

        function myFunction() {


            var audDuration = Math.round(audio.duration*1000);
            console.log("audDuration: "+audDuration);   

            if(document.getElementById("gap").checked){
                console.log("lecture avec "+delaytab[x]+" de temps additionel en ms");  
                audDuration+=delaytab[x];
            }           






            //var curTime = audio.duration;

            setTimeout(function(){
console.log("b: "+b+" llang: "+llang+", rep: "+rep+", seq.join: "+seq.join(","));   
                if(x==(ln-1)){


                    maktab();
                }
                if(x<(ln-1)){
                    x++;
                    conv=parseInt(seq[x]);
                    parseA(conv);
                }   


            }, audDuration);

        }           

    }

    function shuffle(o){
        for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
        return o;
    }   
    </script>

  </head>

  <body>
    Auditory | <a href=visual.html>Visual</a> | <a href=string.html>Whole string Visual</a>
    <hr>
    <div id="page"></div>
    <div id="cheat"></div>
  </body>

</html>

0 个答案:

没有答案