Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize Audio lazily in web builds #137

Merged
merged 1 commit into from
Jul 4, 2024

Conversation

white-axe
Copy link
Collaborator

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

  • Run cargo fmt.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown -Z build-std=std,panic_abort
  • Run cargo build --release
  • If applicable, run trunk build --release

@white-axe white-axe requested a review from a team as a code owner July 4, 2024 15:57
@melody-rs melody-rs merged commit 2b365d9 into Astrabit-ST:dev Jul 4, 2024
4 checks passed
@white-axe white-axe deleted the wasm-lazy-audio branch July 4, 2024 21:02
MolassesLover pushed a commit to MolassesLover/luminol-molasses that referenced this pull request Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants