-
Notifications
You must be signed in to change notification settings - Fork 50
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
No audio in WASM build #124
Comments
Did some digging and I see this popup in the console:
Then I found this issue about for the bevy_kira_audio plugin which describes a script to fix this: So then I added that <!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<title>The Den of the Withered Cherry Tree</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
}
canvas {
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<link data-trunk rel="inline" href="./sound.js"/>
<canvas id="canvas" width="640" height="480"></canvas>
<script src="./rl.js"></script>
<script>
window.addEventListener("load", async () => {
await wasm_bindgen("./rl_bg.wasm");
});
</script>
</body>
</html> // Insert hack to make sound autoplay on Chrome as soon as the user interacts with the tab:
// https://developers.google.com/web/updates/2018/11/web-audio-autoplay#moving-forward
// the following function keeps track of all AudioContexts and resumes them on the first user
// interaction with the page. If the function is called and all contexts are already running,
// it will remove itself from all event listeners.
(function () {
// An array of all contexts to resume on the page
const audioContextList = [];
// An array of various user interaction events we should listen for
const userInputEventNames = [
"click",
"contextmenu",
"auxclick",
"dblclick",
"mousedown",
"mouseup",
"pointerup",
"touchend",
"keydown",
"keyup",
];
// A proxy object to intercept AudioContexts and
// add them to the array for tracking and resuming later
self.AudioContext = new Proxy(self.AudioContext, {
construct(target, args) {
const result = new target(...args);
audioContextList.push(result);
return result;
},
});
// To resume all AudioContexts being tracked
function resumeAllContexts(_event) {
let count = 0;
audioContextList.forEach((context) => {
if (context.state !== "running") {
console.log("resuming an audiocontext");
context.resume();
} else {
count++;
}
});
// If all the AudioContexts have now resumed then we unbind all
// the event listeners from the page to prevent unnecessary resume attempts
// Checking count > 0 ensures that the user interaction happens AFTER the game started up
if (count > 0 && count === audioContextList.length) {
userInputEventNames.forEach((eventName) => {
document.removeEventListener(eventName, resumeAllContexts);
});
}
}
// We bind the resume function for each user interaction
// event on the page
userInputEventNames.forEach((eventName) => {
document.addEventListener(eventName, resumeAllContexts);
});
})(); But I still get the same issue 🤔 Not sure of an easy way I can test to see if the sound not playing is due to specifically this "autoplay" blocking feature, or something else. |
If you're still seeing that console message, then I'd bet that it is autoplay blocking. What happens if you actually wait for a user gesture? |
I'm not getting sound even when I click on the page which should be counting as a gesture. I even tried adding a button so I'm actually clicking something. I'm beginning to suspect the other library I'm using, bracket-lib, tries to do something with gestures which might be interfering with the "don't autoplay audio" logic that exists in browsers now. This codebase was for a game jam and I didn't quite have time to dig deep into the wasm/web build issues. I also multithreaded loading audio which doesn't play nice with wasm 😅 so I need to do some refactoring before I can troubleshoot this further. |
Hello, first off, loving this library 🙂
I'm struggling to get a wasm build that actually plays sounds. I'm not getting any errors or exceptions when using
include_bytes!
and a simple playing of a StaticSound that way. It is odd since I hear things fine for a desktop build, but not for wasm. Since I'm not getting any errors or panics from Kira while playing the sound, I'm wondering if this is an issue with something particular about my wasm build setup.Thanks for looking!
The text was updated successfully, but these errors were encountered: