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
Add a ment  | 

2 Answers 2

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