Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何使这个JavaScript工作以触发声音文件?_Javascript_Audio - Fatal编程技术网

我如何使这个JavaScript工作以触发声音文件?

我如何使这个JavaScript工作以触发声音文件?,javascript,audio,Javascript,Audio,我将此javascript放在head标记中: <script language="javascript" type="text/javascript"> function playSound(soundfile) { document.getElementById("dummy").innerHTML= "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";

我将此javascript放在head标记中:

<script language="javascript" type="text/javascript">
function playSound(soundfile) {
document.getElementById("dummy").innerHTML=
"<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}
</script>

功能播放声音(声音文件){
document.getElementById(“dummy”).innerHTML=
"";
}
我将以下代码放在页面中间的一个div中:

<span id="dummy"></span>
<a href="javascript: void(0);" onclick="playSound('singing-bowl.wav');"><img id="bowl"       
src="pics/singing-bowl.gif" height="119" width="157" /></a>

它不起作用了。我不太懂javascript,但这应该是从我复制的源代码中得到的。我用mp3版本和wav版本都试过了。两个都不适合我

有什么线索吗?

你可以试试这个

function playSound(val)
{
   var sound = new Audio(val);
   sound.addEventListener('canplaythrough', function() { 
     // your call back function
       // for example to play the sound
       sound.play();
   }, false);
}
提高效率

var sound =null;
var isReady = false;
window.onload = function()
{
   sound = new Audio('singing-bowl.wav');
   sound.addEventListener('canplaythrough', function() { 
        isReady = true;
   }, false);

}
function playSound()
{
    if(isReady)
    sound.play();
}

如果您没有支持旧浏览器的要求,我建议您使用html5

<audio controls autoplay>
  <source src="horse.ogg" type="audio/ogg">
  <source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>