HTML5 version is broken in Chrome 66+

In Chrome 66 some changes were introduced that impact whole WebAudio API: https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio so now it’s impossible to start playing before “user interaction” (I personally don’t agree with their policy on this)

So currently my game (compiled with emscripten) just get’s a warning in Chrome dev tools “The AudioContext was not allowed to start. It must be resume (or created) after a user gesture on the page. https://goo.gl/7K7WLu

I probably can workaround it from JS side, though would be nice if FMOD was aware of this :slight_smile:

FMOD already handles this case, you can see in the JS examples that there is an iOS check, and using ‘window.addEventListener(‘touchend’, resumeAudio, false);’ the resumeAudio function calls setDriver(0) to enable the audio again.

You can simply add a mouse onclick check for this as well, ie

var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
if (iOS)
{
    window.addEventListener('touchend', resumeAudio, false);
}
else
{
    document.addEventListener('click', resumeAudio);
}

The only issue with this is that there looks like there is a bug if your device is already ‘trusted’ (and the audio is actually playing) and if the resumeAudio function is called, it erroneously triggers another copy of the audio context, which pulls the fmod mixer twice as fast, and you get stuttering audio.

I guess a hack around that would be to detect if the audio is stalled or not. System::getCPUUsage with a ‘dsp’ = 0 would be an indicator that the audio is stopped, or playing a sound and the position not incrementing is another way.

Either way the issue is fixed in the next release and is a bit cleaner, ie the sample code now uses System::mixerSuspend/mixerResume instead of setDriver(0);

Ie

// Set up iOS/Chrome workaround.  Webaudio is not allowed to start unless screen is touched or button is clicked.
function resumeAudio() 
{
    if (!gAudioResumed)
    {
        console.log("Resetting audio driver based on user input.");

        result = gSystem.mixerSuspend();
        CHECK_RESULT(result);
        result = gSystem.mixerResume();
        CHECK_RESULT(result);

        gAudioResumed = true;
    }

}

var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
if (iOS)
{
    window.addEventListener('touchend', resumeAudio, false);
}
else
{
    document.addEventListener('click', resumeAudio);
}

If you would like an advanced copy of the lib you can contact support. It will come out officially at the end of the month. The examples at www.fmod.org/public/js have already been updated.

Hi Brett! Thanks for you answer.

Do you have any suggestion for us who use fmod as C/C++ library from C/C++ code and compile that with emscripten? It’s not obvious from C++ how to access gSystem variable.