libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.9k stars 1.83k forks source link

Audio not working with emscripten #6385

Closed rokups closed 2 years ago

rokups commented 2 years ago

Firefox produces following warning and audio fails to play:

An AudioContext was prevented from starting automatically. It must be created or resumed after a user gesture on the page.

A bit of search indicates that this is a new browser restriction, which prevents AudioContext from being initialized outside of user event callbacks.

Firefox v105.0.2 SDL v2.24.0

icculus commented 2 years ago

Yeah, you need to gate your app behind some sort of click to start it, as browsers no longer let you start audio before the user has manually interacted with it.

Like the "click or tap to begin" screen here:

https://icculus.org/~icculus/emscripten/flappy-audio/

We could delay audio init, but it makes things more complex, risks devices failing later when we report them as successfully opened, won't help you if you have a passive media player the user doesn't interact with and it's only a matter of time before browsers move other features behind this same requirement anyhow.

The code in that example that handles this is here, in the startClickToPlay function:

https://icculus.org/~icculus/emscripten/flappy-audio/dragonruby-html5-loader.js

It puts up a temporary UI to wait for a click, deletes that UI, then hands off to an Emscripten app that uses SDL2.

Feel free to steal that code.

rokups commented 2 years ago

I suspected as much... Is this documented anywhere? I bet i wont be last one wondering how to approach this.

Thanks for code to steal too! :pray:

slouken commented 2 years ago

Reopened for documentation

icculus commented 2 years ago

Added a note, but this makes me want to write more comprehensive documentation for the platform, which I will do sometime after 2.26.0 ships, I hope.