- Upon entering the site, the element
greetings = document.createElement("audio"); greetings.src = "sounds/greetings.mp3";greetings = document.createElement("audio"); greetings.src = "sounds/greetings.mp3"; - Then the sound is included in the
window.onload.window.onload() { greeting.play(); } - An error is
Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.:Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
How to fight?
- Tried to set
onfocuson thewindow- did not help. - I tried
greetings.pause(); greetings.currentTime = 0; greetings.play();greetings.pause(); greetings.currentTime = 0; greetings.play();- did not help. - I tried to combine the first and second method - it did not help.
From hopelessness I tried:
var greetingsStatus = setInterval(function() { try { greeting.play(); clearInterval(greetingsStatus); greetingsStatus = null; } catch(e) { console.log(e); } }, 200);
The error is also: Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
Good people, help: what to do, what would the sound played immediately after opening the page? Even if it is opened via ctrl or mouse wheel. (The user has not yet visited the page, but the sound is already playing).
PS Google Chrome Version 66.0.3359.139 (Official build), (64 bit)
PS3 While choosing labels for the question: a hint has emerged: web-audio. This is about the audio API. I don't know him yet. Maybe someone knows: will it be easier to solve my problem with him?
The error occurs only when you start the site on the local server. When I switch from stack overflow to my site, everything works fine. Explain, then, how chrome works so that chrome does not trust websites running from a local server, but it doesn’t even trust websites on the Internet.
If you have a different browser, or another version of chrome, and you do not turn on the sound or an error appears in the console - unsubscribe.