Last updated: Mar 7, 2024
Reading time·3 min

To solve the "Uncaught (in promise) DOMException: play() failed because the
user didn't interact with the document first" error, set the muted attribute
on the video element to true. When autoplay is enabled, the video
element has to be muted.

Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
Here is an example of how to set the muted attribute on the video element to
true to enable autoplay.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <video controls width="250" src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/webm" title="Example Video" autoplay="true" muted="true" ></video> <script src="index.js"></script> </body> </html>
The autoplay attribute only works if the muted attribute is set to true.
As this section of the Chrome docs states:
If you have a video element and a
source element, make sure to set the muted attribute on the video element.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <video controls width="250" autoplay="true" muted="true"> <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/webm" /> </video> <script src="index.js"></script> </body> </html>
If you get the error when using Angular, you can set the muted attribute to
true programmatically.
playVideo() { const media = this.videoplayer.nativeElement; media.muted = true; media.play(); }

Here is an example that sets the muted attribute on a video element to
true programmatically in JavaScript.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <video id="video" controls width="250"> <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/webm" /> </video> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const videoEl = document.getElementById('video'); videoEl.muted = true; videoEl.play();
You can also use a mouseover event, but once you set the video element to
not be muted, the video stops playing and requires the user to click once to
play with sound.
const videoEl = document.getElementById('video'); videoEl.muted = true; videoEl.play(); document.body.addEventListener('mouseover', () => { videoEl.muted = false; });
When you load the page:
Alternatively, you can start the video when the user clicks on a different element, e.g. a button.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <button id="btn">Play video with sound</button> <video id="video" controls width="350"> <source src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/webm" /> </video> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const btn = document.getElementById('btn'); const videoEl = document.getElementById('video'); btn.addEventListener('click', event => { videoEl.play(); });
When the user clicks on the button, the video plays with audio.
muted attribute to true because the user has interacted with the domain by clicking on the button.You can also start the video player with a delay after the user clicks on the button.
const btn = document.getElementById('btn'); const videoEl = document.getElementById('video'); btn.addEventListener('click', event => { videoEl.play().then(() => videoEl.pause()); setTimeout(() => videoEl.play(), 2000); });
When the user clicks on the button, the video player starts and is then paused.
After a 2-second delay, the video starts playing with audio again.
You can also use a button to start playing an audio element with sound.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body style="margin-left: 500px; margin-top: 200px"> <button id="btn">Play audio with sound</button> <figure> <figcaption>Listen to the T-Rex:</figcaption> <audio id="audio" controls src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" ></audio> </figure> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const audioEl = document.getElementById('audio'); btn.addEventListener('click', event => { audioEl.play(); });
Once the user clicks on the button, the audio element starts playing with sound.