How to implement automatic audio playback in Chrome browser
P粉617597173
2023-08-20 17:12:40
<p>Audio autoplay works in Mozilla, Microsoft Edge, and older versions of Google Chrome, but cannot autoplay in Google Chrome 67 due to changes in the autoplay policy. </p>
<p>They have blocked autoplay (until certain session conditions specified in the linked blog post are met). How to implement audio autoplay in Google Chrome 67? </p>
Solution #1
My solution was to create an
iframe
and
audio
tags for non-Chrome browsersand in my
script
Solution #2:
According to @Leonard, there is another solution
Create an
iframe
that doesn't play anything, just to trigger autoplay on first load.Good mp3 file source: silence.mp3
Then play your real audio files easily.
Personally prefer Solution #2 as it is a cleaner approach and less dependent on JavaScript.
Updated in August 2019
Solution #3
As an alternative we can use
<embed>
ForFirefox It seems audio autoplay is working so we don't need the
<embed>
element as it will create two audios running at the same time.If your audio has a toggle event, make sure to delete the
<embed>
element you created.NOTE: After the switch it will start from the beginning since
<embed>
has been removed and the<audio>
element will now play normally .Now make sure to hide these
<audio>
and<embed>
elementsNote:
diplay: none
andvisibility: hidden
will make the<embed>
element not work.