I have an animation project that needs to autoplay the audio on all main browsers includes those on mobiles. We also need to control the audio to make it continue playing after it paused and played again. That means we can't use iframe
because it will replay the audio everytime. Plus, just find out that iframe
can't autoplay in Chrome now... Will there be any workaround to fix this problem?

- 125
- 1
- 4
- 11
-
`make it continue playing after it paused` - but its paused – Jaromanda X Sep 19 '19 at 02:39
-
sorry, I mean after it plays again. I just edit the question. – Nicole Sep 19 '19 at 02:41
3 Answers
What all these browsers ask is that your user provide a gesture to confirm they actually want your page to produce sound.
So all you need to do is to invite your users to make this user-gesture.
To be completely safe, you'll call the play()
method of the MediaElement you wish to control from this user-gesture triggered event (that is actually required by Safari), and then you'll have full control over this MediaElement:
// original samples from wikimedia
// https://en.wikipedia.org/wiki/Category:The_Beatles_audio_samples
const audio = new Audio("https://dl.dropboxusercontent.com/s/0eio842lwj1x5dj/Strawberry_Fields_Forever_%28Beatles_song_-_sample%29.mp3");
audio.repeat = true;
audio.volume = 0;
document.getElementById( 'btn' ).onclick = e => {
document.getElementById( 'splash' ).remove();
audio.play().then( () => {
audio.currentTime = 0;
audio.pause();
audio.volume = 1;
startTimers();
});
};
function startTimers() {
setTimeout( playAudio, 2000 );
setTimeout( pauseAudio, 3000 );
setTimeout( playAudio, 6000 );
setTimeout( changeSrc, 7000 );
setTimeout( pauseAudio, 15000 );
setTimeout( playAudio, 60000 );
setTimeout( pauseAudio, 70000 );
}
function playAudio() {
console.log('playing');
audio.play();
}
function pauseAudio() {
console.log('pausing');
audio.pause();
}
function changeSrc() {
console.log('changing src');
audio.src = "https://dl.dropboxusercontent.com/s/okbnk1ycupxrrb2/_Sgt._Pepper%27s_Lonely_Hearts_Club_Band_by_the_Beatles_1967%20%281%29%20%28online-audio-converter.com%29.mp3";
audio.play();
}
#splash {
position: absolute;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
#splash p { display: inline-block }
<div id="splash">
<button id="btn"><h1>Start the awesome</h1></button>
</div>

- 123,334
- 13
- 219
- 285
-
Yes, I know we need to make the user to triger the audio to make it play. But is it possible to skip the trigger thing and make the audio autoplay itself? – Nicole Sep 19 '19 at 06:11
-
-
@Kaiido So how is this web page doing it: http://trump.tv/video/2GlqpgCRIYg – Lemuel Uhuru Jan 09 '21 at 03:24
-
@SirLemuel nothing plays automatically there for me. Maybe you did allow YT to autoplay videos in your browser's settings? – Kaiido Jan 09 '21 at 03:26
-
@Kaiido Wow. Thanks for the speedy response. I'm not sure exactly but it does play automatically which is the first time I've seen that. If it doesn't work for you then I'll have to troubleshoot further but I just randomly decided to see if there was a Trump.tv and it redirected to that page and played the video instantly. – Lemuel Uhuru Jan 09 '21 at 03:29
-
1
-
Probably they kept the interaction you had with the page that did redirect you there. Can you try typing the address in the address bar of a new tab? – Kaiido Jan 09 '21 at 03:36
-
how does this one manage the autoplay ? https://olafwempe.com/ – Garbez François Jan 20 '22 at 12:51
There's no more autoplaing audio on browsers. I recommend reading these.:
Google's article Autoplay policy chghanges
MDN's Autoplay guide
Webkits autoplay policy
There's a lot of good information, why it works as it does nowadays and what to do for it.

- 302
- 2
- 10
When you attempt to autoplay audio without the user interacting with the document first, it will throw an error. So the key is to get the user interact with the document first.
What you can do is, when user opens the page, he must first click somewhere or some button to make the actual content visible and then with that play the audio after the click interaction.
For catching the error see Extend the DOM Element, Handle the Error, and Degrade Gracefully.
PS: I noticed when switching tab, reload the tab you switched to and switch back+reload it just works without any workaround. Not a solution, i just wonder why this happens (i am using latest Chrome). It must trigger document interaction without doing anything