Search Unity

Chrome On The Dev Branch (version 66) May Block Audio For Unity WebGL

Discussion in 'Web' started by kognito1, Feb 15, 2018.

  1. Erik-Sombroek

    Erik-Sombroek

    Joined:
    Jul 21, 2015
    Posts:
    10
    To give even more contexts, its after the download of the unityweb file is complete and its trying to parse it. I do have to note that this is in a web environment where i don't support gzip, so i guess its falling back on the Unity Gzip decompression. Probably the bottleneck lies here.
     
  2. Erik-Sombroek

    Erik-Sombroek

    Joined:
    Jul 21, 2015
    Posts:
    10
    Ok :) i have verified the issue, in my testing environment i didn't set Content-Encoding: gzip on unityweb files. And for some reason the fallback to the internal decompressor breaks it, but i solved it on my end by setting the encoding!
     
  3. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    This story is so funny.

    First, because google have annouced the change in September 2017 ==> https://developers.google.com/web/updates/2017/09/autoplay-policy-changes and no one of the unity staff have worked on it.

    The warning has been present since a long time in google canary version, so that mean that the unity staff do not test anything on the beta version of the web browser vendors. If i'm wrong then this situation would never have happened :)

    Kognito1 have made this post 3 months ago ^^

    Now, one and a half years later (after the google announcement), we are at the same point. It's normal for you?

    I respect you guy but Schubkraft tell us "There is currently no workaround, but we're still looking into it." and i found a workaround myself 24 hours after. Marco-Trivellato (thank you for finding this workaround) give us a solution that should work but hard to setup for for no exerimented user and no tested in production.

    This kind of situations should never happen because the web browser vendors give us the tools (beta version) and information at the right time.

    A new black screen and texture bug is present in the developer version of firefox (not sure yet if the bug come from firefox or unity). I can bet 50 dollars that if i do not report it myself, Unity or firefox (according from where come the bug) will wake up when everybody will be affected :)

    I have hear that the unity team work closely with the web browser vendors. I must say that it's a complete fail when we see that something like this happen.

    Anyway, writting a complet book will not change anything.

    I love all of you guy but something like that should never happen. But in same time, you can't work on the Unity 2048 and on the current Unity version in same time :) It's not your fault (i hope) but the fault of your boss.

    Have a good day !

    Your lovely grumpy french monkey :)
     
    dradb likes this.
  4. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Glad to hear it works now.

    For future reference and consistency, you may want to use the same tool we use to compress in our toolchain. Try the 7z.exe or 7za (depending on whether you are Windows or Mac) in the Tools folder (<Unity install folder>\Editor\Data\Tools on Windows), passing the following arguments: a -tgzip <target> <source>
     
  5. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    A bug report would be really appreciated.
     
  6. blacKode

    blacKode

    Joined:
    May 21, 2015
    Posts:
    38
    Works with any event, not necessarily a click (button), i use onmouseover to launch the audio and works fine... try https://gzone.pro
     
  7. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    Sorry, but your solution is not 100 % reliable.

    https://imgur.com/a/kagSizT

    First image, only the background music is stoped and button sound work.
    Second image : backround and button sound are not working.

    My system work in 100% of the case, you must review your testing process.
     
  8. moakesy

    moakesy

    Joined:
    Sep 12, 2012
    Posts:
    5
    Does anybody know of a mac solution for the compressing/uncompressing? I'm getting "Invalid or unexpected token" on Chrome and "SyntaxError: illegal character" on Firefox. I assume that it's an issue with the encoding like Erik had, but I can't find 7za on mac to solve it.
     
  9. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    <Unity install folder>/Unity.app/Contents/Tools/7za
     
    moakesy likes this.
  10. Erik-Sombroek

    Erik-Sombroek

    Joined:
    Jul 21, 2015
    Posts:
    10
    Thanks! I will look into the 7za tool, its doing a better job than regular gzip :)

    If anyone is interested in the shell script, when your as lazy as i am and you have to fix many games:
    (confirmed working on MAC)

    Code (CSharp):
    1. #!/bin/sh
    2. FILE=$(find . -name '*.asm.framework.unityweb' -exec basename {} +)
    3. echo 'fixed '$FILE
    4. mv $FILE $FILE.gz
    5. gunzip $FILE.gz
    6. gsed -i.bak 's#function _JS_Sound_Init(){try{window.AudioContext=window.AudioContext||window.webkitAudioContext;WEBAudio.audioContext=new AudioContext;WEBAudio.audioWebEnabled=1}catch(e){alert("Web Audio API is not supported in this browser")}}#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{clearInterval(resumeInterval)}};var resumeInterval=setInterval(tryToResumeAudioContext,400);WEBAudio.audioWebEnabled=1}catch(e){alert("Web Audio API is not supported in this browser")}}#g' $FILE
    7. rm $FILE.bak
    8. gzip --best $FILE
    9. mv $FILE.gz $FILE
    10.  
    Some steps to make this work:
    save the above file as fixUnityAudio.sh
    Run chmod +x fixUnityAudio.sh to mark it a shell script
    Navigate to your Build folder and run fixUnityAudio.sh and your script should be fixed!
     
    Last edited: May 9, 2018
  11. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    FWIW, here is an alternative approach (unmute button) that does not require you to change <buildname>.framework.unityweb. As you can see there are multiple possible solutions to the problem.

    BTW, you can also find other people commenting on the new Chrome policy here.
     
    Last edited: May 9, 2018
  12. moakesy

    moakesy

    Joined:
    Sep 12, 2012
    Posts:
    5
    I still can't seem to get this to work. Modifying the file manually or using Erik's shell script still give me the illegal character error. I'm running the build locally so this isn't a web config problem. Do I need to change the 7za settings or anything?
     
  13. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Could you share the patched build? Alternatively, you could try the "unmute button" solution mentioned above.
     
    Last edited: May 9, 2018
  14. moakesy

    moakesy

    Joined:
    Sep 12, 2012
    Posts:
    5
    Unfortunately I can't share it due to NDA, I'll give the unmute button a go though. Thanks
     
  15. SylvesterAgerbekHansen

    SylvesterAgerbekHansen

    Joined:
    Feb 23, 2018
    Posts:
    1
    Do you mind sharing how you created the preloader, I really need it
     
  16. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    Just check the source code of my page and understand what i do.
    I don't want that someone broke his load build just by copying and pasting the code.

    Some adjustment can be done according how you load your game but just by checking the source code of the page, you have the idea and the complet project.
     
  17. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    on the topic of play button approach, I would like to mention that it is possible to instantiate the unity content before displaying the Play Button. See here for an example.
    To achieve that, we use the noInitialRun option that prevents the content from being executed automatically after initialization, then we explicitly run it (respectively in preInit and onRuntimeInitialized) when the button is pressed:
    Code (csharp):
    1. <!DOCTYPE html>
    2. <html lang="en-us">
    3.   <head>
    4.     <meta charset="utf-8">
    5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    6.     <title>Unity WebGL Player | %UNITY_WEB_NAME%</title>
    7.     <link rel="shortcut icon" href="TemplateData/favicon.ico">
    8.     <link rel="stylesheet" href="TemplateData/style.css">
    9.     <script src="TemplateData/UnityProgress.js"></script>
    10.     <script src="%UNITY_WEBGL_LOADER_URL%"></script>
    11.     <script>
    12.       var gameInstance = UnityLoader.instantiate("gameContainer", "%UNITY_WEBGL_BUILD_URL%", {onProgress: UnityProgress,
    13.         Module: {
    14.           preInit: [function(){
    15.             this.gameInstance.Module.noInitialRun = true;
    16.           }],
    17.           onRuntimeInitialized: function() {
    18.             const btn = document.createElement('button');
    19.             btn.classList.add('playButton');
    20.             btn.textContent = 'PLAY';
    21.             btn.style.fontSize = '2em';
    22.             btn.onclick = e => {
    23.               gameInstance.Module.callMain();
    24.               btn.remove();
    25.             };
    26.             this.gameInstance.container.appendChild(btn);
    27.           }
    28.         }
    29.       });
    30.     </script>
    31.   </head>
    32.   <body>
    33.     <div class="webgl-content">
    34.       <div id="gameContainer" style="width: %UNITY_WIDTH%px; height: %UNITY_HEIGHT%px"></div>
    35.       <div class="footer">
    36.         <div class="webgl-logo"></div>
    37.         <div class="fullscreen" onclick="gameInstance.SetFullscreen(1)"></div>
    38.         <div class="title">%UNITY_WEB_NAME%</div>
    39.       </div>
    40.     </div>
    41.   </body>
    42. </html>
    Full WebGLTemplate can be downloaded here.

    For information about noInitialRun/preInit/onRuntimeInitialized please refer to the emscripten documentation., and the Unity manual to know how to use WebGL Templates.
     
    Last edited: May 13, 2018
  18. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    Thank you for this exemple, i'm sure that it will help a lot of guy.

    The only problems "for me" is that one part of the game is downloaded before the button and when we press the button , we have a "freeze/blank" time before the complet load. It's just my 2 cents just for my point of view.

    But this exemple will be perfect for a lot of peoples.
     
  19. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    You might be experiencing a freeze because of engine initialization + first scene loading.
     
  20. lbranjord1

    lbranjord1

    Joined:
    Dec 12, 2016
    Posts:
    2
    Hello,

    I am just adding my supporting vote that this is very important to an organization i am working with as well. We have a QA team who will all be working around it and a client who will be trying the product off and on in the coming weeks. I know there is always a lot to do but this is a very important issue for us. Thank you!

    -Logan
     
  21. lbranjord1

    lbranjord1

    Joined:
    Dec 12, 2016
    Posts:
    2
    We discovered that if you click on the page while the webgl app is loading, it WILL load audio for you. Just in case you need to try it. Not a viable workaround but a neat one.
     
  22. Derek-Pettigrew

    Derek-Pettigrew

    Joined:
    May 15, 2018
    Posts:
    3
    I've implemented the above function _JS_Sound_Init() replacement solution in a PostProcessBuild function, but have hit problems generating a new *.asm.framework.unityweb file. The fix works if I do not gzip the file - all good. However, when I gzip it (using the Unity tools 7za a -tgzip approach suggested above), the game will error on loading with the invalid character error also reported above.

    I have tracked the problem down to the newly generated .gz file NOT having the following comment in the gzip header block - 'UnityWeb Compressed Content (gzip)'. I can manually edit the binary file to add this comment, and it all works as expected. I'd rather not have to do this, and wondered what tool Unity is using to insert this comment when the file is originally packaged?
     
  23. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    Yes just edit the Audio.js file as described here.

    To be clear: you only need to edit the .unityweb file itself if you do not want to make a new build. If you're making a new build just edit the Audio.js file in your Unity installation folder.
     
  24. Derek-Pettigrew

    Derek-Pettigrew

    Joined:
    May 15, 2018
    Posts:
    3
    We'd rather not do that, as we are a large team, and would need to make that change on all development machines and build machines.
     
  25. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    I can appreciate the challenge. I just sent the Audio.js file as an attachment with instructions on how to find the original and replace it with the new one. It didn't seem like a big deal. But we also only really *need* the fix on our build machine. All other builds are more or less prototypes which we can workaround (ignore) the chrome issue. Hopefully the fix is coming to a patch release soon. :)
     
  26. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    game_apps likes this.
  27. Krazune

    Krazune

    Joined:
    Nov 17, 2015
    Posts:
    8
    I changed the "JS_Sound_Init()" in the Audio.js file, and then I tried to build an older project, but nothing changed. I even went ahead and erased the contents of the Audio.js file, and I still got audio playing in the newest build. Every change to the Audio.js is ignored when I rebuild the project, what am I doing wrong?
     
  28. Derek-Pettigrew

    Derek-Pettigrew

    Joined:
    May 15, 2018
    Posts:
    3
    Check that your Chrome hasn't been updated already. Google patched it last night, and this problem has gone away for the time being.
     
  29. Vicomte

    Vicomte

    Joined:
    Sep 18, 2013
    Posts:
    5
    I'm getting this as well, I have edited the Audio.js file in the unity installation folder as instructed. Then I rebuilt the game.
    However, if I unzip the framework file and read it, I see the OLD function present and not the new one I put in Audio.js.

    Any idea whats up with that?
     
    Last edited: May 16, 2018
  30. Krazune

    Krazune

    Joined:
    Nov 17, 2015
    Posts:
    8
    Updating chrome to a newer version does remove the problem, but I shouldn't expect users to have that version.

    I was using version 66.0.3359.170, in which the audio is still blocked if no input is given, however, my problem is that Unity is not using Audio.js to build my project, because any changes made to that file are ignored, as I said above.
     
  31. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
  32. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
  33. patrick-kulling

    patrick-kulling

    Joined:
    Feb 18, 2016
    Posts:
    7
    Hey Marco,
    is there an ETA when the next 2017.4 LTS is coming out?

    Best,
    Patrick
     
  34. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    Today ;)
     
  35. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    @Marco-Trivellato
    We still have the warning message :
    "The Web Audio autoplay policy will be re-enabled in Chrome 70 (October 2018). Please check that your website is compatible with it. https://goo.gl/7K7WLu" even by using the version 2018.2.5f1. It's normal?
     
  36. Schubkraft

    Schubkraft

    Unity Technologies

    Joined:
    Dec 3, 2012
    Posts:
    1,073
    Afaik this is a warning from Chrome to remind people about this. We patched it on our end in 2017.4.6 LTS and all latest 2018.X. Download Chrome 70 (current dev channel) and test it against your project using the mentioned Unity versions.

    If it doesn't work for your case using the described setup please file a bug report and get back to us here. Thanks!
     
  37. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    Tested on :

    Unity 2018.1.2f1 and 2018.2.5f1
    Chrome Canary : 70.0.3538.0

    Test link (Default setup): https://www.asr-games.net/Testing/Terrain/

    The warning is still present.

    Thank you
     
    Last edited: Aug 31, 2018
  38. Schubkraft

    Schubkraft

    Unity Technologies

    Joined:
    Dec 3, 2012
    Posts:
    1,073
    The warning will always be present. The question is whether your audio works the way it does on Chrome 69 or are there any issues?
     
  39. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    598
    Hum sorry, i have misunderstood. I have tested 2/3 game (because the mine use a button/gesture) and it seems that everything is working correctly.
     
  40. Marco-Trivellato

    Marco-Trivellato

    Unity Technologies

    Joined:
    Jul 9, 2013
    Posts:
    1,654
    It is normal. This is a reminder from Google that the policy will be re-enabled in October. The only thing that Unity does at the moment is resume audio upon user gesture.

    We strongly recommend users to implement a flow that works around this problem (e.g.: play button style flow)
     
  41. unity3dat

    unity3dat

    Joined:
    Aug 9, 2017
    Posts:
    88
    I have the exact same issue: audio only starts after 15 seconds or so. This only occurs in Chrome, it wasn't occuring before, and it only occurs in wasm builds, asm builds are fine. I've updated to the latest Unity, but that didn't fix the issue.
     
  42. truizz

    truizz

    Joined:
    Jul 17, 2015
    Posts:
    2
  43. truizz

    truizz

    Joined:
    Jul 17, 2015
    Posts:
    2
  44. pedroabgmarques

    pedroabgmarques

    Joined:
    Jun 21, 2017
    Posts:
    2
    Tried everything in this thread and elsewhere, nothing seems to works flawlessly with Safari 12 on macOS.
     
  45. TheRoccoB

    TheRoccoB

    Joined:
    Jun 29, 2017
    Posts:
    54
    You can try adding this code. I forget whether it needs to be called before or after UnityLoader.instantiate, but probably before is best.

    This starts the sound on the first click or keypress in the game.

    You can see it in action by going to any game on SIMMER.io and checking out any game w/ audio https://simmer.io/@QuangHaiVu/marble-adventure

    It's important to note that you need to go directly to a game and not click around in the UI first to get a real use case.

    Let me know if this works!

    It basically hijacks the audioContext and starts audio on Gesture.

    Code (JavaScript):
    1.   function startAudioOnClick(){
    2.  
    3.     window.AudioContext = window.AudioContext || window.webkitAudioContext;
    4.     var bind = Function.bind;
    5.     var unbind = bind.bind(bind);
    6.  
    7.     function instantiate(constructor, args) {
    8.       return new (unbind(constructor, null).apply(null, args));
    9.     }
    10.  
    11.     window.AudioContext = function (AudioContext) {
    12.       return function () {
    13.         var audioContext = instantiate(AudioContext, arguments);
    14.         window.myAudioContext = audioContext;
    15.         console.log('AudioContext has been instantiated!');
    16.         return audioContext;
    17.       }
    18.     }(AudioContext);
    19.  
    20.     var webAudioEnabled = false;
    21.  
    22.     function resumeAudio(){
    23.       if (!webAudioEnabled && window.myAudioContext){
    24.         console.log('SIMMER starting Audio!');
    25.         window.myAudioContext.resume();
    26.         webAudioEnabled = true;
    27.       }
    28.     }
    29.  
    30.     document.body.addEventListener('click', resumeAudio, true);
    31.     document.addEventListener('keydown', resumeAudio, true);
    32.   }
     
    rendermouse, Psyco92, lkzero and 3 others like this.
  46. mikewilliams76901

    mikewilliams76901

    Joined:
    Jan 29, 2014
    Posts:
    3

    This works! Thank you!
     
  47. TheRoccoB

    TheRoccoB

    Joined:
    Jun 29, 2017
    Posts:
    54
    Excellent @mikewilliams76901 great to have confirmation that it's working for somebody else.
     
  48. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    It works for us too! I’ve been off on vacation for a bit but my coworkers report this broke Chrome for us (perhaps our unique setup?) so we only use it if the browser is Safari 12 or later. Many thanks though!
     
    TheRoccoB likes this.
  49. outlaw6666

    outlaw6666

    Joined:
    Mar 20, 2014
    Posts:
    1
    I got the same problem on iOS platform with Safari Web browser. I have tried the solution from TheRoccoB, but it can't work. Anyone has a solution for it?
     
  50. shilpa12

    shilpa12

    Joined:
    May 10, 2018
    Posts:
    1
    Hi,
    I tried the solution too using unity 2018.2. But it doesn't work for me. Any help?