1. Upon entering the site, the element greetings = document.createElement("audio"); greetings.src = "sounds/greetings.mp3"; greetings = document.createElement("audio"); greetings.src = "sounds/greetings.mp3";
  2. Then the sound is included in the window.onload . window.onload() { greeting.play(); }
  3. 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 onfocus on the window - 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.

  • one
    Such code will not work for you in Safari either. This is a kind of protection that the sound would not play until the user makes the page active. Web audio will help in chrome, but will not help in Safari. - Stepan Kasyanenko
  • @StepanKasyanenko error appears only when starting from a local server. If you go to a site that is hosting, an error does not occur. It is strange that the browser does not trust the sites running from a local server, and trusts the sites on the Internet. here - Don2Quixote
  • No, do not trust. prntscr.com/jepb6u - Stepan Kasyanenko
  • Most users immediately close such noisy tabs, consider whether this is really what you need. - vp_arth
  • Here, by the way, it describes quite well why, why, and how it should be developers.google.com/web/updates/2017/09/… - Stepan Kasyanenko

1 answer 1

Use jQuery trigger. For example, you have 2 buttons on the play and pause site. You can make it so that when you load the page in chrome, you clicked on the play button

Here is the HTML:

 <div class="musicPlayer"> <audio class="listen" preload="none" data-size="50" src="music_url" id="audio0"></audio> <button class="playerButtons playMusic"><i class="fas fa-play"></i></button> <button class="playerButtons pauseMusic"><i class="fas fa-pause"></i></button> </div> 

Here is jS

 $(window).on('load', function(){ $(".playMusic").trigger('click'); }) 
  • it didn't help me personally - Vitaly Fesyura
  • @ VitaliyFesyura Everything depends on the browser. If the browser has disabled the inclusion of the default, the script will not help you. - Elman Huseynov