The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on t (729113)

My WebGL game opens to a 2 second clip from the videoPlayer. However, it doesn’t start playing, I just see Frame 1.
I get this error message in the Google Chrome Developer console:

Try it here:

http://www.roseportalgames.com/modules/Memory Game/index.html

I have to click on the game, go to a different tab and go back to the game tab, then it will play. It’s just very weird, I don’t understand it and I don’t know how to fix it?

Help would be much appreciated. :slight_smile:

I am getting the same issue even after revmoing all the audio listener. Did you solve the problem?

This is a new “feature” in most browsers now. In order to avoid annoying users with audio that starts playing on its own somewhere on the page, the user must now click on the window that has the audio in order for the web audio context to become available. Unfortunately it doesn’t just start playing on its own after you click, you have to write some javascript that manually resumes it in a click handler.

From the mozilla developer website:

susresBtn.onclick = function() {
  if(audioCtx.state === 'running') {
    audioCtx.suspend().then(function() {
      susresBtn.textContent = 'Resume context';
    });
  } else if(audioCtx.state === 'suspended') {
    audioCtx.resume().then(function() {
      susresBtn.textContent = 'Suspend context';
    }); 
  }
}

I’m curious if anyone knows if there are plans to build this resume functionality into the Unity WebGL compiler at some point in the future so it doesn’t need to be done manually by the developer?

3 Likes

I have this issue in Unity 2020 WebGL build.
I don’t have this issue in my old WebGL build see http://www.neix.eu/RuntimeAudioClipLoader/demo/WebGL/index.html

Try out latest Unity 2021.1.0b5 whether that has the issue. There was a recent patch that landed to help this issue. I am curious to know if it still happens in 2021.1.0b5 or newer. (we are looking to backport to earlier releases as well, but I think that has not yet happened)

1 Like

@jukka_j If it’s any help, I get this in 2020.2.2f. Will give it a try with latest 2021.1

@jukka_j tried with 2021.1.0b5. It fixed the issue, along with another annoying issue (rendering inconsistencies between webgl and other platforms).

Hope both fixes makes it into 2020.2 and/or 2020.3 (lts).

That is great to hear!

There are audio playback patches in the queue to earlier Unity versions, I hope it is one of them that fixed it. If you are unable to update in the short term, ping back in a couple of Unity patch releases if the issue is not fixed then.

2 Likes

@jukka_j There is something weird going on… I tried 2020.2.3f1 as well, just to make sure it wasn’t already fixed, and it seemed to work, then I downgraded back to 2020.2.2f1 and it still worked.

Maybe it matters where the game is hosted, like maybe it works with localhost, but not where I host it (gitlab pages and itch.io)? I didn’t do a new clone when downgrading from 2020.2.3 to 2020.2.2, though, so perhaps it’s just some cache or files from the newer version still lying around in the project folder?

Unfortunately, my CI setup (https://hub.docker.com/r/unityci/editor/tag) doesn’t support beta releases, so I can’t really test with 2021.1.0b5 in staging, but I’ll give it a try with 2020.2.3 and see if that helps, then do some fresh clones + reimport of my project locally to rule out some of the things I mentioned above.

I have the same issue on 2019.4.8f1. I do not use any audio in my project.

@jukka_j I suspect my issues are due to this bug: No sound clips in WebGL build

My CI solution runs on linux and my local builds are on windows, which would neatly explain why it works in local builds but not in production/staging.

Seems like the warning messages about a missing AudioContext were just a red herring in my case…

TL;DR Any (hacky) workarounds I could try on Unity 2017? ( @jukka_j )

Just run into this problem porting a project that uses 2017.2.
Checking, LTS releases page says “LTS Release 2017.4.0f1” was “Released: 20 March 2018” and they get support until “two years after the initial release date”. That makes it 20 March 2020 = no fix in 2017.
I assume I need to present my client with choices:

  • upgrade the project
  • live with the bug

Are there any (hack) solutions I could investigate? Perhaps something javascripty? (especially since this is asm.js)
Specifically we’ve already done work to delay all 'userland’ audio until the ‘right time’ but from the logs, there’s something happening before we do any audio! Here’s an excerpt of the log:

log excerpt

c7977da2-1395-49ba-9ebc-7581aa2e7acb:2 Initialize engine version: 2017.2.0f3 (46dda1414e51)

UnityLoader.js:1 Creating WebGL 2.0 context.
c7977da2-1395-49ba-9ebc-7581aa2e7acb:2 Renderer: WebKit WebGL

c7977da2-1395-49ba-9ebc-7581aa2e7acb:2 Vendor: WebKit

c7977da2-1395-49ba-9ebc-7581aa2e7acb:2 Version: OpenGL ES 3.0 (WebGL 2.0 (OpenGL ES 3.0 Chromium))

c7977da2-1395-49ba-9ebc-7581aa2e7acb:2 GLES: 3

c7977da2-1395-49ba-9ebc-7581aa2e7acb:2 EXT_color_buffer_float GL_EXT_color_buffer_float EXT_color_buffer_half_float GL_EXT_color_buffer_half_float EXT_disjoint_timer_query_webgl2 GL_EXT_disjoint_timer_query_webgl2 EXT_float_blend GL_EXT_float_blend EXT_texture_compression_bptc GL_EXT_texture_compression_bptc EXT_texture_compression_rgtc GL_EXT_texture_compression_rgtc EXT_texture_filter_anisotropic GL_EXT_texture_filter_anisotropic EXT_texture_norm16 GL_EXT_texture_norm16 KHR_parallel_shader_compile GL_KHR_parallel_shader_compile OES_texture_float_linear GL_OES_texture_float_linear WEBGL_compressed_texture_s3tc GL_WEBGL_compressed_texture_s3tc WEBGL_compressed_texture_s3tc_srgb GL_WEBGL_compressed_texture_s3tc_srgb WEBGL_debug_renderer_info GL_WEBGL_debug_renderer_info WEBGL_debug_shaders GL_WEBGL_debug_shaders WEBGL_lose_context GL_WEBGL_lose_context WEBGL_multi_draw GL_WEBGL_multi_draw OVR_multiview2 GL_OVR_multiview2

c7977da2-1395-49ba-9ebc-7581aa2e7acb:2 OPENGL LOG: Creating OpenGL ES 3.0 graphics device ; Context level <OpenGL ES 3.0> ; Context handle 1

The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
c7977da2-1395-49ba-9ebc-7581aa2e7acb:2 UnloadTime: 96.420000 ms

It seems only after all of that do we get the first calls to any Awake() methods.
It seems like something in Unity is doing some audio work before our code takes control. Any pointers welcome!

Thanks!

p.s. In case it’s of relevance, we’re doing this in the context of the AirConsole game framework.
p.p.s. When this happens, it also results in performance slowly degrading. We start at 60FPS and, within a few minutes, it has degraded to 30FPS and keeps going down.

Unfortunately 2017.4 is indeed no longer receiving updates. I think the patches landed into the file “Audio.js” under the PlaybackEngines directory tree inside Unity installation. You can try cross-referencing the new Audio.js playback code there with the old one if you are in a pinch.

The relevant machinery that is supposed to enable audio playback after a user gesture has occurred is this code:

        var tryToResumeAudioContext = function() {
            if (WEBAudio.audioContext.state === 'suspended')
                WEBAudio.audioContext.resume();
            else
                Module.clearInterval(resumeInterval);
        };
        var resumeInterval = Module.setInterval(tryToResumeAudioContext, 400);

You can try to check if Unity 2017.4 has that kind of logic in it, and apply that in the JS code if not.

1 Like

That’s awesome! Thanks for replying. I’ll definitely try hacking it into our version and seeing if it helps!

Still an issue in 2021.1.12f1, just console spam that I have to figure out how to get rid of.

2 Likes

We are getting it spamming to the console in 2021.2.3f1. it goes away once you click on the player. Behavior would be better if it just outputted one error then tested silently until it succeeds.

I got same problem, I’ve solve this by disable Audio relative module completely in Package Manager
That include:
Timeline, Director, Video, Unity Web Request Audio, Unity Web Request WWW

Maybe it’s enough just disable Unity Web Request Audio, but in my case I’m not playing audio at all, so I just disable them all
My Unity version is 2020.3.25f1


Not sure if you need do it all, For solving it you can simply go to Edit → Project Settings → Audio and tick the Disable Unity Audio option

I think the main issue is that the game auto starts. I’m using 2020.x

I wrapped the last script tag’s code into a function called startGame, then after that function added:

<script>
function startGame() {
// this function has the code from the final <script> tag
}

var startButton = document.querySelector("#startButton");
startButton.onclick = startGame;
</script>

Then I just threw a button in, after the mobile warning. It doesn’t look good, but I’m testing here.

<div id="unity-mobile-warning">
WebGL builds are not supported on mobile devices.
</div>
<button id="startButton">
Start Game
</button>

Now the game won’t start unless you tell it to, which should get rid of the audio context error. I don’t have audio, yet, just cleaning up errors from upgrading

FYI, the forum code tool absolutely sucks on here. Using the standard code window it kept giving me an unauthorized error. Using the codebb tool kinda works, but nowhere near as nice as the stackoverflow code editor.

3 Likes

Now I can’t edit the post to add a link. You can view source here for the example fix. I also did some style changes, as my added article for how to play was pushing the unity player off the screen. Unity WebGL Player | testing grounds