admin管理员组文章数量:1396094
I want to autoplay audio in my website as soon as I open my site. But it doesn't work.
<audio id="myAudio" autoplay>
<source src="./Wele to Kitchen Nightmares..mp3" type="audio/ogg">
<source src="./Wele to Kitchen Nightmares..mp3" type="audio/mpeg">
</audio>
<script>
function myFunction() {
var x = document.getElementById("myAudio").autoplay;
document.getElementById("demo").innerHTML = x;
}
myFunction();
I want to autoplay audio in my website as soon as I open my site. But it doesn't work.
<audio id="myAudio" autoplay>
<source src="./Wele to Kitchen Nightmares..mp3" type="audio/ogg">
<source src="./Wele to Kitchen Nightmares..mp3" type="audio/mpeg">
</audio>
<script>
function myFunction() {
var x = document.getElementById("myAudio").autoplay;
document.getElementById("demo").innerHTML = x;
}
myFunction();
Share
Improve this question
asked Dec 10, 2022 at 13:08
VigneshVignesh
11 gold badge1 silver badge1 bronze badge
3
- 2 browser like mozilla block autoplay until user give permission. – Anil kumar Commented Dec 10, 2022 at 13:10
- 1 the autoplay attribute on the audio tag was enough. With valid sources it's working on my firefox with no permissions given. – Diego D Commented Dec 10, 2022 at 13:12
- 1 for further analysis, this is the mdn page about that developer.mozilla/en-US/docs/Web/Media/Autoplay_guide – Diego D Commented Dec 10, 2022 at 13:16
2 Answers
Reset to default 2
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.2.1/jquery.js"></script>
<script>
function PlayMusic() {
var play=document.getElementById("music");
play.play();
}
$(document).ready(function(){
setTimeout(PlayMusic,3000);
})
</script>
</head>
<body>
<audio controls id="music" >
<source src="https://www.puterhope./jargon/m/example.mp3" type="audio/mpeg">
</audio>
</body>
</html>
After page load audio will play (1 sec delay).
URL The URL of the audio file. Possible values:
An absolute URL - points to another web site (like src="http://www.example./horse.ogg")
A relative URL - points to a file within a web site (like src="horse.ogg")
Should start playing the audio file when the page loads, thanks to the autoplay
attribute on the <audio>
element. The buttons in the HTML code allow you to control the audio playback by calling the play()
and pause()
methods on the <audio>
element.
Note that some browsers may not allow audio to automatically play on a website due to user experience and security concerns. In these cases, the user may need to explicitly start the audio playback by clicking on a button or some other element on the page.
<audio id="myAudio" autoplay>
<source src="./Wele to Kitchen Nightmares..mp3" type="audio/ogg">
<source src="./Wele to Kitchen Nightmares..mp3" type="audio/mpeg">
</audio>
<p>
<button onclick="playAudio()">Play Audio</button>
<button onclick="pauseAudio()">Pause Audio</button>
</p>
<script>
function playAudio() {
var audio = document.getElementById("myAudio");
audio.play();
}
function pauseAudio() {
var audio = document.getElementById("myAudio");
audio.pause();
}
</script>
本文标签: javascriptIs there a way to autoplay audio in htmlStack Overflow
版权声明:本文标题:javascript - Is there a way to autoplay audio in html? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744679248a2619298.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论