InvalidStateError: Failed to execute 'resume' on 'AudioContext': Cannot resume a closed AudioContext

8459390--1123010--upload_2022-9-22_23-53-0.png

The issue is on this function on build.framework.js

function _JS_Sound_Init() {
        try {
            window.AudioContext = window.AudioContext || window.webkitAudioContext;
            WEBAudio.audioContext = new AudioContext;
            var tryToResumeAudioContext = function() {
                if (WEBAudio.audioContext.state === "suspended") WEBAudio.audioContext.resume();
                else Module.clearInterval(resumeInterval)
            };
            var resumeInterval = Module.setInterval(tryToResumeAudioContext, 400);
            WEBAudio.audioWebEnabled = 1;
            var _userEventCallback = function() {
                try {
                    if (WEBAudio.audioContext.state !== "running") {
                        WEBAudio.audioContext.resume()
                    }
                    jsAudioPlayBlockedAudios();
                    var audioCacheSize = 20;
                    while (WEBAudio.audioCache.length < audioCacheSize) {
                        var audio = new Audio;
                        audio.autoplay = false;
                        WEBAudio.audioCache.push(audio)
                    }
                } catch (e) {}
            };
            window.addEventListener("mousedown", _userEventCallback);
            window.addEventListener("touchstart", _userEventCallback);
            Module.deinitializers.push(function() {
                window.removeEventListener("mousedown", _userEventCallback);
                window.removeEventListener("touchstart", _userEventCallback)
            })
        } catch (e) {
            alert("Web Audio API is not supported in this browser")
        }
    }

The try catch doesn’t catch the error on WEBAudio.audioContext.resume() . The reason why it’s not catched is because resume returns a promise. The way I fixed the problem locally is changing the line that comes before to

if (WEBAudio.audioContext.state !== "running" && WEBAudio.audioContext.state !== "closed"), but catching the promise also works.

Unfortunately I can only reproduce the issue when using a plugin that messes with sound. It’s also a regression, since the problem happens on Unity 2021 and 2022 but not on 2020 or 2019.

What do you think @brendanduncan_u3d ? Can I report this bug with the sample project including the plugin without it being publicly available on the issue tracker? It’d be better if I didn’t have to report this bug, and the promise was simply properly catched…Thanks!

I’ll point the issue out to the dev that’s been doing a lot of work with the audio code lately.

1 Like

Hi,
I am the dev in question :slight_smile: You are correct the error handling seems wrong and this needs investigation. I will create a bug ticket. Can you give me some information on how to reproduce this? What is the name of the audio plugin that you are using? I think that the audio context is never closed in Unity that’s why we haven’t encountered the error before. Is the audio plugin closing and recreating the audio context?

The plugin in question is RecorderWebGL(I’m the author). I created a file, RecorderWebGL2021.jslib to patch the issue on Unity 2021 and later. To replicate the issue, you will need to use Unity 2021 or higher, and follow the “RECORDING IN GAME AUDIOS” section of the documentation, and in the example scene, attempt to record the game with in game audio. After you stop the recording and tap anywhere on the screen, the error will show.

If you want to I can create an example scene and pm you, that’s probably easier.

Hi! I’ll go OT but it’s related to audio and context.
In our build, as soon as the page is loaded there are “The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.” warnings until user interact with the page. I know it’s a browser limitation but I find it annoying (and i don’t know the performance implications).
I’ve tried different things, even without Audio Listener (and, obviously, neither Audio Source) I get the same warning. The only way not to have it is by disabling unity audio in project settings. Am I missing something or there is something wrong you can fix?

Hi @Marks4 ,

I send you a DM but haven’t heard back from you yet. A example scene would be very nice to recreate this bug.

Best wishes

Marcel

Sorry I don’t have time at the moment. I already explained why it doesn’t work. If you can force the audio context state to closed when trying to resume, the error will happen. If you just take into account that resume returns a promise and then().catch() it will solve the problem.

Hi,

Sorry for the late update on the issue. The bug has been fixed now. You can find the bug tracking link with the Unity version the bug was fixed in here: Unity Issue Tracker - [WebGL] InvalidStateError: Failed to execute &#39;resume&#39; on &#39;AudioContext&#39;: Cannot resume a closed AudioContext.

Best wishes
Marcel

1 Like