HTML5音频不会在Safari中触发/工作

时间:2012-10-05 18:03:39

标签: html5-audio safari

以下代码适用于除Safari以外的所有浏览器(对于Windows 7使用版本5.1.7)。

var texto01 = document.getElementById("texto01");
texto01.onkeyup = function(tecla){
    if(tecla.keyCode == 13){    //If Enter is keyup
        if((valorAnt[0] != texto01.value) && (texto01.value != "")){    //Check to see if answer has changed and field is not empty
            valorAnt[0] = texto01.value;    //Since value has change, set it to be new previous answer
            if(texto01.value == "5747"){    //Correct answer user has to type
                ctx.clearRect(739, 57, 35, 35);
                ctx.drawImage(bienImg, 739, 57);    //Tick image
                var cBien = Math.ceil(Math.random()*2); //There are two audio files that can be played if answer is correct
                var cSonidoB = "bien" + cBien;
                document.getElementById(cSonidoB).play();   //play the "random" audio for right answer
            }
            else{ //Answer is wrong
                ctx.clearRect(739, 57, 35, 35);
                ctx.drawImage(malImg, 739, 57); //X mark image
                var cMal = Math.ceil(Math.random()*3);  //There are three audio files that can be played if answer is wrong
                var cSonidoM = "mal" + cMal;
                document.getElementById(cSonidoM).play();   //play the "random" audio for wrong answer
            }
        }
    }
}//texto01

所有音频标签都声明如下:

<audio id = "mal1" preload = "auto"> 
    <source src="Sounds/mal01.ogg" type="audio/ogg"></source>
    <source src="Sounds/mal01.mp3" type="audio/mpeg"></source>
</audio>

我尝试过以下方法但没有成功: 1)将MP3源放在OGG之前。 2)将类型更改为“audio / mp3” 3)删除选择播放哪个音频的“随机”部分,并直接指定要播放的音频ID。

1 个答案:

答案 0 :(得分:0)

也许您必须在.htaccess中添加一些代码,如下所示:

AddType audio/mpeg mp3
AddType audio/mp4 m4a
AddType audio/ogg ogg
AddType audio/ogg oga
AddType audio/webm webma
AddType audio/wav wav

AddType video/mp4 mp4
AddType video/mp4 m4v
AddType video/ogg ogv
AddType video/webm webm
AddType video/webm webmv

AddType audio/mpeg .mp3
AddType audio/mp4 .m4a
AddType audio/ogg .ogg
AddType audio/ogg .oga
AddType audio/webm .webma
AddType audio/wav .wav

AddType video/mp4 .mp4
AddType video/mp4 .m4v
AddType video/ogg .ogv
AddType video/webm .webm
AddType video/webm .webmv