-1

Ex. https://www.bunlongheng.com/sound/police-4

I don't know why the audio is not playing automatically. Is it the restriction by my browser or my code is wrong? I tested in Chrome & Firefox.

<audio id="myAudio" controls autoplay>
  
    <source src="/assets/sounds/police-4.ogg" type="audio/ogg">
   

  <source src="/assets/sounds/police-4.mp3" type="audio/mpeg">

  Your browser does not support the audio element.
</audio>

As you can see, I've already added autoplay


Click play work, but I need to make it auto-play.

code-8
  • 54,650
  • 106
  • 352
  • 604
  • “*I'm working on a personal project to trigger a URL with sound when a button is pressed.*” If it’s on a button press, why not just link that event to a JavaScript function that plays the sound rather than relying on `autoplay` which is disabled in many browsers because it is fundamentally bad UX? – esqew Mar 08 '22 at 22:51
  • But the issue is why the audio tag is not autoplay as expected. When I pressed a smart button triggering Alexa routine to open a URL, and that URL need to autoplay the sound. What 's bad about that UX ? It's perfectly how it suppose to be. – code-8 Mar 08 '22 at 22:53
  • @esqew I updated my post to reduce the confusion, I get your point. But I don't refer to the HTML – code-8 Mar 08 '22 at 22:56

1 Answers1

0

Chromium based internet browsers do not support autoplay. Although to bypass this you can set it to muted autoplay by doing

<audio id="myAudio" controls autoplay muted>

But the audio will not automatically play. The user must manually select the mute button for it to start playing.

For more info visit W3Schools

blvuxe
  • 21
  • I need auto play. Why u answer like this ? – code-8 Mar 09 '22 at 01:32
  • To clarify what blvuxe is trying to say: modern browsers don't allow you to start playing audio automatically; this is so websites will not start blaring sound as soon as you open them. Adding both the `autoplay` and `muted` attributes will make the audiofile start playing, but you won't hear anything (as it is muted). This is something you see done with video's a lot, where they play but you need to click somewhere to turn on the sound. Some user interaction (click, keyboard) is always needed to unmute your audio/video player. This is a technical limitation added by the browser developers. – Jasper Jun 10 '22 at 11:31