HTML5 Audio doesn't work anymore in Chrome: The AudioContext was not allowed to start.

Ricardo

Member
Hey guys!

Using runtime 2.1.4.288 and Chrome 66.0.3359.139 (PC, Windows 10), I'm getting an error like this as soon as my game starts:
The AudioContext was not allowed to start. It must be resume (or created) after a user gesture on the page.

Then, as you can expect, audio doesn't work in the game. This kind of issue was well-know in HTML5 mobile games, but this limitation for Desktops appears to be a very recent change in Chrome's Autoplay Policy Changes.

Does anyone know a workaround for this or have some info? I have some big projects in production affected by this issue and the old trick (play a sound in a click/gesture event) doesn't seems to work as the AudioContext doesn't even start and it crash during the game loading, even before the application starts.
Re-build my projects right now using a old runtime (this bug doesn't affect runtime 2.1.4.189) isn't also a good option for me at the moment, unfortunately...
 

chmod777

Member
It has happened by the end of 2017 in Chrome Canary.

The behavior has changed many times since then, but yeah a user gesture is now required in some cases (especially on iframes, if the game is played on domain X, but hosted on domain Y).

Code:
if(typeof AudioContext != "undefined" || typeof webkitAudioContext != "undefined") {
   var resumeAudio = function() {
      if(typeof g_WebAudioContext == "undefined" || g_WebAudioContext == null) return;
      if(g_WebAudioContext.state == "suspended") g_WebAudioContext.resume();
      document.removeEventListener("click", resumeAudio);
   };
   document.addEventListener("click", resumeAudio);
}
 

True Valhalla

Full-Time Developer
GMC Elder
It has happened by the end of 2017 in Chrome Canary.

The behavior has changed many times since then, but yeah a user gesture is now required in some cases (especially on iframes, if the game is played on domain X, but hosted on domain Y).

Code:
if(typeof AudioContext != "undefined" || typeof webkitAudioContext != "undefined") {
   var resumeAudio = function() {
      if(typeof g_WebAudioContext == "undefined" || g_WebAudioContext == null) return;
      if(g_WebAudioContext.state == "suspended") g_WebAudioContext.resume();
      document.removeEventListener("click", resumeAudio);
   };
   document.addEventListener("click", resumeAudio);
}
Thanks for sharing.
 

Ricardo

Member
It has happened by the end of 2017 in Chrome Canary.

The behavior has changed many times since then, but yeah a user gesture is now required in some cases (especially on iframes, if the game is played on domain X, but hosted on domain Y).

Code:
if(typeof AudioContext != "undefined" || typeof webkitAudioContext != "undefined") {
   var resumeAudio = function() {
      if(typeof g_WebAudioContext == "undefined" || g_WebAudioContext == null) return;
      if(g_WebAudioContext.state == "suspended") g_WebAudioContext.resume();
      document.removeEventListener("click", resumeAudio);
   };
   document.addEventListener("click", resumeAudio);
}
Awesome workaround, thanks a lot for sharing it.
 
I

ilqar

Guest
It has happened by the end of 2017 in Chrome Canary.

The behavior has changed many times since then, but yeah a user gesture is now required in some cases (especially on iframes, if the game is played on domain X, but hosted on domain Y).

Code:
if(typeof AudioContext != "undefined" || typeof webkitAudioContext != "undefined") {
   var resumeAudio = function() {
      if(typeof g_WebAudioContext == "undefined" || g_WebAudioContext == null) return;
      if(g_WebAudioContext.state == "suspended") g_WebAudioContext.resume();
      document.removeEventListener("click", resumeAudio);
   };
   document.addEventListener("click", resumeAudio);
}
where to enter this script
 

True Valhalla

Full-Time Developer
GMC Elder
https://www.theverge.com/2018/5/15/17358752/google-chrome-66-web-audio-api-sound-broken-game-change-delay-apology

"Google is partially and temporarily rolling back a recent Chrome change that blocked autoplaying audio, after web developers complained that it had broken countless games and apps. The update rolled out with Chrome version 66 in early May, and it was intended to quiet annoying ads and videos that would drive users toward ad-blocking software. But it also ended up completely removing the audio from interactive web projects that relied on specific commands, which created problems for artists — and doomed any abandoned projects to silence."

Some good news, however, they still plan to reintroduce the behavior in Chrome 70.
 
Last edited:

Ricardo

Member
Always and when chmod777's workaround continues to work, I'll be happy. Hopefully YoYo will implement a fix for this at some point.
 
Top