Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'WebGL' started by Marks4, Sep 23, 2022.

  1. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    491
    upload_2022-9-22_23-53-0.png


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

    Code (JavaScript):
    1. function _JS_Sound_Init() {
    2.         try {
    3.             window.AudioContext = window.AudioContext || window.webkitAudioContext;
    4.             WEBAudio.audioContext = new AudioContext;
    5.             var tryToResumeAudioContext = function() {
    6.                 if (WEBAudio.audioContext.state === "suspended") WEBAudio.audioContext.resume();
    7.                 else Module.clearInterval(resumeInterval)
    8.             };
    9.             var resumeInterval = Module.setInterval(tryToResumeAudioContext, 400);
    10.             WEBAudio.audioWebEnabled = 1;
    11.             var _userEventCallback = function() {
    12.                 try {
    13.                     if (WEBAudio.audioContext.state !== "running") {
    14.                         WEBAudio.audioContext.resume()
    15.                     }
    16.                     jsAudioPlayBlockedAudios();
    17.                     var audioCacheSize = 20;
    18.                     while (WEBAudio.audioCache.length < audioCacheSize) {
    19.                         var audio = new Audio;
    20.                         audio.autoplay = false;
    21.                         WEBAudio.audioCache.push(audio)
    22.                     }
    23.                 } catch (e) {}
    24.             };
    25.             window.addEventListener("mousedown", _userEventCallback);
    26.             window.addEventListener("touchstart", _userEventCallback);
    27.             Module.deinitializers.push(function() {
    28.                 window.removeEventListener("mousedown", _userEventCallback);
    29.                 window.removeEventListener("touchstart", _userEventCallback)
    30.             })
    31.         } catch (e) {
    32.             alert("Web Audio API is not supported in this browser")
    33.         }
    34.     }
    The
    Code (JavaScript):
    1. try catch
    doesn't catch the error on
    Code (JavaScript):
    1. 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

    Code (CSharp):
    1. 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!
     
    Last edited: Sep 23, 2022
  2. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    334
    I'll point the issue out to the dev that's been doing a lot of work with the audio code lately.
     
    Marks4 likes this.
  3. MarcelPursche

    MarcelPursche

    Unity Technologies

    Joined:
    Mar 3, 2021
    Posts:
    44
    Hi,
    I am the dev in question :) 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?
     
  4. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    491

    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.
     
  5. DudoTheStampede

    DudoTheStampede

    Joined:
    Mar 16, 2015
    Posts:
    79
    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?
     
  6. MarcelPursche

    MarcelPursche

    Unity Technologies

    Joined:
    Mar 3, 2021
    Posts:
    44
    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
     
  7. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    491
    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.
     
  8. MarcelPursche

    MarcelPursche

    Unity Technologies

    Joined:
    Mar 3, 2021
    Posts:
    44
    Marks4 likes this.