在 html 中加载页面后播放音频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14356956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Playing audio after the page loads in html
提问by Priya Sunanthan
I inserted the audio in html. But the audio gets started before the entire page loads. I want the audio to be played after the entire page gets loaded.
我在 html 中插入了音频。但是音频在整个页面加载之前就开始了。我希望在整个页面加载后播放音频。
<audio src="bg.mp3" autoplay="autoplay" loop="loop"></audio>
Can anyone help me in this.
任何人都可以帮助我。
回答by Aioros
You're going to need JavaScript for that. Remove the autoplay
attribute:
为此,您将需要 JavaScript。删除autoplay
属性:
<audio id="my_audio" src="bg.mp3" loop="loop"></audio>
and add a script like this:
并添加这样的脚本:
window.onload = function() {
document.getElementById("my_audio").play();
}
Or if you use jQuery:
或者,如果您使用 jQuery:
$(document).ready(function() {
$("#my_audio").get(0).play();
});
回答by Rajesh
Just Copy and Paste this Code in Body section of your HTML Code.
只需将此代码复制并粘贴到 HTML 代码的正文部分即可。
<audio autoplay>
<source src="song.mp3" type="audio/mpeg">
</audio>
and make sure that your audio file should be in same folder.
并确保您的音频文件应位于同一文件夹中。
回答by Sebastian Koziara
I think you must use JS (jQuery) function to do get document ready
我认为您必须使用 JS (jQuery) 函数来准备文档
$(document).ready(function()
$(document).ready(function()
Play an audio file using jQuery when a button is clicked
You have answer here (if I follow you right)
你在这里有答案(如果我正确地跟随你)
回答by Trinadh Koya
For a repeated tone place the below code
对于重复的音调,请放置以下代码
<audio src="./audio/preloader.mp3" autoplay="autoplay" loop="loop"></audio>
and for a single time, remove the loop. Then it should be like this
一次,删除循环。那么它应该是这样的
<audio src="./audio/preloader.mp3" autoplay="autoplay"></audio>