Initialize Audio
lazily in web builds
#137
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
We were having problems with the sound test in web builds since #114 where if the user doesn't click on the canvas or press a key while the canvas is focused between when the page started and finished loading, the sound test would not be able to play any sound because the Web Audio API requires an "activation triggering input event" to play audio in most browsers.
The fork of cpal we were using before #114 avoided this problem by repeatedly attempting to create an audio context until the user interacted with the application. After #114, cpal only tries once to create an audio context and just gives up if the first attempt was unsuccessful.
This pull request works around this problem by deferring the creation of an audio context in web builds until a sound is played for the first time.
Testing
The bug can be reproduced in web builds by reloading the page and then not interacting with the application for about 1 second after the interface starts rendering. Playing a sound in the sound test window would then not play any sound unless the changes from this pull request are used.
Checklist
cargo fmt
.cargo clippy
. If applicable, add:--target wasm32-unknown-unknown -Z build-std=std,panic_abort
cargo build --release
trunk build --release