Skip to content

Commit

Permalink
Fix audio context not restarting automatically (#249)
Browse files Browse the repository at this point in the history
# Objective

The script for restarting the audio context for web apps didn't work.
This resulted in the app not getting any audio.

# Solution

The fix is simple: The script for restarting the audio context needs to
be called _before_ the app is loaded, I assume to ensure that the
creation of the audio context is tracked correctly.

# Testing

You can test it on the current prototype of the [`bevy_new_2d`
port](TheBevyFlock/bevy_new_2d#312).

Compare `bevy run --no-default-features web --open` with the current
`main` and this branch.
This branch should have sound when you click the buttons.
  • Loading branch information
TimJentzsch authored Jan 29, 2025
1 parent aba26bf commit d3e7939
Showing 1 changed file with 42 additions and 41 deletions.
83 changes: 42 additions & 41 deletions assets/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,8 @@
</div>

<script type="module">
// Starting the game
// The template uses `bevy_app.js`, which will be replaced by the name of the generated JS entrypoint when creating the local web server
import init from "./build/bevy_app.js";
init().catch((error) => {
if (
!error.message.startsWith(
"Using exceptions for control flow, don't mind me. This isn't actually an error!"
)
) {
throw error;
}
});
</script>

<script type="module">
// Hide loading screen when the game starts.
const loading_screen = document.getElementById("loading-screen");
const observer = new MutationObserver((records) => {
for (const record of records) {
for (const addedNode of record.addedNodes) {
if (addedNode instanceof HTMLCanvasElement) {
// A new canvas has been created, which means that the game has been loaded
// Hide the loading screen!
loading_screen.style.display = "none";
observer.disconnect();
return;
}
}
}
});

observer.observe(document.body, {
subtree: false,
childList: true,
attributes: false,
characterData: false,
});
</script>

<script type="module">
// Script to restart the audio context
// Automatically restart the audio context after user interaction
// Needs to be executed _before_ the game is loaded
// Taken from https://developer.chrome.com/blog/web-audio-autoplay/#moving-forward
(function () {
// An array of all contexts to resume on the page
Expand Down Expand Up @@ -163,5 +124,45 @@
});
})();
</script>

<script type="module">
// Starting the game
// The template uses `bevy_app.js`, which will be replaced by the name of the generated JS entrypoint when creating the local web server
import init from "./build/bevy_app.js";
init().catch((error) => {
if (
!error.message.startsWith(
"Using exceptions for control flow, don't mind me. This isn't actually an error!"
)
) {
throw error;
}
});
</script>

<script type="module">
// Hide loading screen when the game starts.
const loading_screen = document.getElementById("loading-screen");
const observer = new MutationObserver((records) => {
for (const record of records) {
for (const addedNode of record.addedNodes) {
if (addedNode instanceof HTMLCanvasElement) {
// A new canvas has been created, which means that the game has been loaded
// Hide the loading screen!
loading_screen.style.display = "none";
observer.disconnect();
return;
}
}
}
});

observer.observe(document.body, {
subtree: false,
childList: true,
attributes: false,
characterData: false,
});
</script>
</body>
</html>

0 comments on commit d3e7939

Please sign in to comment.