简体   繁体   English

用HTML预加载我的音频文件

[英]Preloading my audio file in html

I'm currently using the below way to play sound effect on my website. 我目前正在使用以下方式在我的网站上播放声音效果。 I need the sound to play out everytime I received a protocol from my slave device: 每当我从从属设备收到协议时,我都需要播放声音:

<script>
function RxProtocol()
{
    playSound('audio123.wav');
}

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

<body>
<span id="dummy"></span>
</body>

But seems like this way the audio played is delayed everytime. 但是好像这样每次播放音频都会延迟。 After I received my protocol for 2 seconds, the audio only played. 收到协议2秒钟后,仅播放音频。

Is it because I did not preload my audio? 是因为我没有预加载音频吗? Could you guys teach me how to modify my code and make it into preload enable? 你们可以教我如何修改我的代码并使之进入预加载功能吗?

You can try preload attribute of html5 audio tag 您可以尝试html5 audio tag preload attribute

like preload="auto" 就像preload="auto"

also read http://webdesignledger.com/tips/audio-and-video-with-html5 and http://code.google.com/p/html5-preloader/ 另请阅读http://webdesignledger.com/tips/audio-and-video-with-html5http://code.google.com/p/html5-preloader/

Somehow my below coding worked for firefox and chrome but not for IE9: 我的下面的代码以某种方式适用于Firefox和Chrome,但不适用于IE9:

<script>
function RxProtocol()
 {
    var a = document.getElementById("audio1");
    a.play();
 }

</script>

<body>
<audio id="audio1">
<source src="audio.wav" type="audio/wav">
<source src="audio.mp3" type="audio/mpeg">
audio tag not supported.
</audio>
</body>

After trying and trying, I found out that I need to put the below in order for IE9 to work. 经过反复尝试,我发现我需要在下面放置内容以使IE9正常工作。 Firefox and Chrome worked too: Firefox和Chrome也可以使用:

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

Before this, I put the below coding which worked on Firefox and Chrome only: 在此之前,我将下面的代码仅适用于Firefox和Chrome:

 <meta http-equiv="Pragma" content="no-cache"/>

Do you guys know what is the difference of these two meta? 你们知道这两个meta有什么区别吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM