Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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

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

  1. jon_miller

    jon_miller

    Joined:
    Apr 23, 2014
    Posts:
    1
    Hello @TheRoccoB
    I've tried adding your solution to both before and after "UnityLoader.instantiate", but in neither case can I get the sound to work on my Unity game. I notice that the sound on the Unity apps do work on your site (simmer.io), so I'm wondering why this isn't working for me. Do you have a new solution? One thing I notice is that your console logs which instantiate the audio are not being displayed, which means those functions are not called. And I've added my own console.logs as a sanity check to ensure that Safari isn't pulling from an older cached version.

    My unmodified solution plays audio on chrome, so I don't believe the issue is on my end.

    To anyone that got this to work, did you simply paste this script in and is it still working for you?

    quick edit: I'm using Unity version 2018.3.0f2

    Thank you!
     
  2. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    Forgot to mention, this actually works for iOS too but you need to add an ontouchstart event listener also.
     
  3. ravishnair

    ravishnair

    Joined:
    Aug 8, 2016
    Posts:
    20
    I have this issue as well, (as in the audio plays on all other platforms except Safari12) and realized that if we open up the game on browser as a standalone, the audio plays. There is no audio when opening it as a facebook app, however. IS this something people have faced? I am using 2018.3.6f1.

    I tried the above solution and that didnt seem to do anything, sadly.
     
  4. yankow

    yankow

    Joined:
    Apr 10, 2018
    Posts:
    7
    Ah well I just tried the solution from @TheRoccoB but wasn't sure about in which file to place it, and where. I have tried in the index.html file as a staring point, right before " var gameInstance = UnityLoader.instantiate", but so far no luck. Sorry for being such a noob but can anybody give me a hint? Thanks a lot in advance!
     
    yehielc likes this.
  5. gavila_pico

    gavila_pico

    Joined:
    Feb 17, 2018
    Posts:
    1
    I'm using Unity 2018.2.3f1 with FMOD 1.10, I'm having the same issue as everyone else in this forum. I used @TheRoccoB solution, but it doesn't work everytime. Are there any news from Unity on this topic? Because I see that the script has been changed in Unity, but it's still showing the AudioContext not allowed to start. I had to force an interaction before playing a sound, but still won't work everytime. I think there should be a function inside Unity that would allow the AudioContext to resume.
     
    Last edited: Mar 26, 2019
  6. MFKJ

    MFKJ

    Joined:
    May 13, 2015
    Posts:
    264
    I am surprised that one year old problem still not fixed, I am using unity 2018.3.8 and getting this problem:
    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
     
  7. Schubkraft

    Schubkraft

    Unity Technologies

    Joined:
    Dec 3, 2012
    Posts:
    1,078
    That is the way browsers work now with audio. If the user does click/gesture into the page the audio context will start.
     
  8. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Listen, I'm about to pay for a musician to make music for my game, and I'm flabbergasted I can't get a straight answer on this.

    Does audio work on WebGL, or not?
     
  9. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    What does that mean, click/gesture? If my whole page is the Unity WebGL canvas, and they click on the canvas, will audio play? There's a huge difference between clicking on an HTML element that isn't the canvas, and interacting with the canvas. Just tell me what I need to do.
     
  10. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    A click/gesture means you have to physically click onto the WebGL game, anywhere. It doesn't have to be a button. So yes, clicking on the canvas will cause audio to play. I have this working on my WebGL game https://www.TheLastAntLion.com with Chrome Version 74.0.3729.131 (Official Build) (64-bit)

    You can go there and try it out to see what I'm talking about.
     
    Last edited: May 6, 2019
  11. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Unfortunately, on Safari 12.1, no sound plays!

    The underlying issue is that a touch start or mouse press event must be on the call stack of the code that eventually plays an audio context, or audio will not play. The underlying implementation of input in WebGL puts a touch/click event on a queue, that is later dequeued by a player thread and therefore does not have the event on the call stack.

    So it's not as simple as you're describing.
     
    Last edited: May 6, 2019
  12. lkzero

    lkzero

    Joined:
    May 16, 2019
    Posts:
    1
    Good jobs, it's working. Thanks @TheRoccoB
     
  13. yehielc

    yehielc

    Joined:
    Oct 1, 2018
    Posts:
    3
    its been a while but can you be more specific where i need to place this code?!? tnx!
     
  14. cursoproder

    cursoproder

    Joined:
    Nov 6, 2019
    Posts:
    1
    Thanks @TheRoccoB .
    It works on Safari 13, but I needed to add
    Code (HTML):
    1. <body onload="startAudioOnClick()">
    in the body tag.
     
  15. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,942
    So I've found myself up against this same issue here in October 2020. None of the audio will start playing in my WebGL window, until I've clicked inside of it. Was there consensus on a way to approach this that works across browsers?
     
    Last edited: Oct 22, 2020
  16. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    This reads like the expected behavior: browsers do block audio autoplay until user interacts with the document, and there is no way to work around it. Or are you seeing some other issue on top of that?
     
    JamesArndt likes this.
  17. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,942
    No that is the exact issue I'm experiencing. I figured it was expected behavior. I wasn't sure if there were any way to work around this? Maybe a way to simulate an interaction and initiate audio playing without the user clicking on the content? My problem is that I'm hovering over these elements in the canvas and audio clips should play, but they don't until I've actually clicked somewhere on the canvas.
     
  18. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    Unfortunately if that were somehow possible, it would be a security violation in the browser that browser vendors would quickly patch up: the intent of the limitation that web browsers impose it to mandate that no page is allowed to play back audio unless the user really interacts with the page. That is to prevent ad popups and background tabs from blasting audio without user's content.
     
  19. skoteskote

    skoteskote

    Joined:
    Feb 15, 2017
    Posts:
    88
    Can't get audio to work in WebGL in Unity 2020.2, on any browser. All works well in the editor, in the browser, I get the error mentioned previously in this thread:

    The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
    On 1.framework.js line 4319:
    WEBAudio.audioContext = new AudioContext;

    and line 4321:
    if (WEBAudio.audioContext.state === "suspended") WEBAudio.audioContext.resume(); else Module.clearInterval(resumeInterval);


    I've made it so AudioSource plays on
    Input.GetMouseButton(0)
    , but guess I'm doing it wrong. All the workarounds mentioned previously in this thread seem to have been implemented in the current Unity. How do I get it to work?

    (Odd thing, if I press the webGL screen on loading the error never appears, but still no audio.)
     
    Last edited: Dec 18, 2020
  20. Fede192

    Fede192

    Joined:
    May 4, 2015
    Posts:
    3
    I'm having the same issue. I get the message "The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page." every second until I click the canvas. But after that there is still no audio whatsoever. I tried Chrome, Firefox and Opera.
     
  21. Fede192

    Fede192

    Joined:
    May 4, 2015
    Posts:
    3
    skoteskote likes this.