Search Unity

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

Discussion in 'Web' started by roseportalgames, Jan 19, 2019.

  1. roseportalgames

    roseportalgames

    Joined:
    Aug 9, 2017
    Posts:
    173
    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. :)
     

    Attached Files:

    Last edited: Jan 19, 2019
  2. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    I am getting the same issue even after revmoing all the audio listener. Did you solve the problem?
     
  3. Logic_Bomb

    Logic_Bomb

    Joined:
    Apr 6, 2017
    Posts:
    16
    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:
    Code (JavaScript):
    1. susresBtn.onclick = function() {
    2.   if(audioCtx.state === 'running') {
    3.     audioCtx.suspend().then(function() {
    4.       susresBtn.textContent = 'Resume context';
    5.     });
    6.   } else if(audioCtx.state === 'suspended') {
    7.     audioCtx.resume().then(function() {
    8.       susresBtn.textContent = 'Suspend context';
    9.     });
    10.   }
    11. }
    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?
     
    Mehrdad995 and JeffreyBennett like this.
  4. JakubNei

    JakubNei

    Joined:
    Jul 20, 2013
    Posts:
    26
  5. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    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)
     
    kashiyama likes this.
  6. bobbaluba

    bobbaluba

    Joined:
    Feb 27, 2013
    Posts:
    81
    @jukka_j If it's any help, I get this in 2020.2.2f. Will give it a try with latest 2021.1
     
  7. bobbaluba

    bobbaluba

    Joined:
    Feb 27, 2013
    Posts:
    81
    @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).
     
  8. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    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.
     
    Arkade and bobbaluba like this.
  9. bobbaluba

    bobbaluba

    Joined:
    Feb 27, 2013
    Posts:
    81
    @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.
     
  10. marcin-huuuge

    marcin-huuuge

    Joined:
    Sep 5, 2018
    Posts:
    15
    I have the same issue on 2019.4.8f1. I do not use any audio in my project.
     
  11. bobbaluba

    bobbaluba

    Joined:
    Feb 27, 2013
    Posts:
    81
  12. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    655
    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:
    1. upgrade the project
    2. 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:

    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. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    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.
     
  13. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    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:

    Code (JavaScript):
    1.         var tryToResumeAudioContext = function() {
    2.             if (WEBAudio.audioContext.state === 'suspended')
    3.                 WEBAudio.audioContext.resume();
    4.             else
    5.                 Module.clearInterval(resumeInterval);
    6.         };
    7.         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.
     
    Arkade likes this.
  14. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    655
    That's awesome! Thanks for replying. I'll definitely try hacking it into our version and seeing if it helps!
     
  15. Blarp

    Blarp

    Joined:
    May 13, 2014
    Posts:
    269
    Still an issue in 2021.1.12f1, just console spam that I have to figure out how to get rid of.
     
    TheElumenati and Just_Lukas like this.
  16. TheElumenati

    TheElumenati

    Joined:
    Jan 22, 2014
    Posts:
    38
    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.
     
  17. Jon-Gao

    Jon-Gao

    Joined:
    Dec 9, 2012
    Posts:
    17
    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
     

    Attached Files:

  18. mes007

    mes007

    Joined:
    Apr 1, 2019
    Posts:
    4
    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
     
    Last edited: Jan 20, 2022
  19. ps2goat

    ps2goat

    Joined:
    Aug 31, 2016
    Posts:
    7
    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:

    ```html
    <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.

    ```html
    <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.
     
    Gaminggoat, qwert024 and makaka-org like this.
  20. ps2goat

    ps2goat

    Joined:
    Aug 31, 2016
    Posts:
    7
  21. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    1,023
    @jukka_j Unity 2021.3.4 LTS still has this issue. I don't understand: why Unity Developer must operate with JavaScript manual patches mentioned above?
     
  22. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    Confirmed on 2021.3.6f1 (released yesterday). Any updates? @jukka_j
     
  23. pedroomedas

    pedroomedas

    Joined:
    Feb 24, 2022
    Posts:
    2
    Where can we find the patches?
     
  24. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Any update on this? I've started getting this issue too now.
    On Unity 2021.3.18f
     
  25. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    Still happens on latest 2022.1.24f1
     
  26. justtime

    justtime

    Joined:
    Oct 6, 2013
    Posts:
    424
    Still happens on latest 2021 LTS
     
  27. RendergonPolygons

    RendergonPolygons

    Joined:
    Oct 9, 2019
    Posts:
    98
    Still happens in Unity 2022.3.4.f1 LTS, error shows on chrome console Version 114.0.5735.199 (Official Build) (64-bit) but sound can be heard
     
  28. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    271
    so to confirm since there are really old messages here: is the issue that the sound is correctly playing, but the console is showing the error message:
    Code (JavaScript):
    1. The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. <URL>
    as soon as the Unity canvas loads?
     
    bugfinders likes this.
  29. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,731
    I get this even before i have a screen that plays sound!! I have a first scene with some text that basically tells them whats changed recently like a patch notes, and things to note, and i get this.. I also get a cant use webcam.. I dont ever need or use webcams nor have i found anywhere to say I want or dont want one..
     
  30. grrava

    grrava

    Joined:
    Nov 11, 2012
    Posts:
    46
    Yes, I indeed have the issue that I get this in the console. For a sound to start playing the user will have to click somewhere in the game and the sounds seem to work, so that's fine. Is there a way to surpress the warning?
     
  31. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Removed the whole sound in my build but still I get the same log.
    Unity 2022.3.10f1.
     
    xucian likes this.
  32. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    is someone actively looking into this or do we have to file a bug report?
    or was it solved already? (I plan to update to the latest 2022 soon)
     
  33. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,731
    well 2023.3.b8 still has this so.. id say no.. (took me like 17 minutes to compile up my darn project)
     
    xucian likes this.