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:
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?
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.
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?
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)
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.
@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.
My CI solution runs on linux and my local builds are on windows, which would neatly explain why it works in local builds but not in production/staging.
Seems like the warning messages about a missing AudioContext were just a red herring in my case…
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:
upgrade the project
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 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.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.
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.
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:
var tryToResumeAudioContext = function() {
if (WEBAudio.audioContext.state === 'suspended')
WEBAudio.audioContext.resume();
else
Module.clearInterval(resumeInterval);
};
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.
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.
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
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:
<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.
<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.
Now I can’t edit the post to add a link. You can view source here for the example fix. I also did some style changes, as my added article for how to play was pushing the unity player off the screen. Unity WebGL Player | testing grounds