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

No audio in WASM build #124

Open
flydecahedron opened this issue Feb 28, 2025 · 3 comments
Open

No audio in WASM build #124

flydecahedron opened this issue Feb 28, 2025 · 3 comments

Comments

@flydecahedron
Copy link

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.

impl Audio {
    pub fn new() -> Self {
        let manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default()).unwrap();
        Self {
            manager,
            sounds: HashMap::new(),
        }
    }

    pub fn play(&mut self, name: &str) -> StaticSoundHandle {
        let sound = self.sounds.get(name).expect("Sound not found");
        let snd = self.manager.play(sound.clone()).unwrap();

        snd
    }

    pub fn load_from_bytes(&mut self, name: &str) {
        let src = include_bytes!("../resources/candle_fire.wav").to_vec();
        let mut sound_bytes = vec![];
        for byte in src {
            sound_bytes.push(byte);
        }

        let sound = StaticSoundData::from_cursor(Cursor::new(sound_bytes))
            .expect("Failed to load sound file");
        self.sounds.insert(name.to_string(), sound);
    }
}
    gs.audio.load_from_bytes("candle");
    gs.audio.play("candle").set_volume(-6.0, Tween::default());
<html>
  <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
  </head>
  <body>
    <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>
cargo build --release --target wasm32-unknown-unknown
wasm-bindgen target/wasm32-unknown-unknown/release/rl.wasm --out-dir wasm --no-modules --no-typescript
[dependencies]
bracket-lib = { git = "https://github.com/amethyst/bracket-lib.git", rev = "851f6f08675444fb6fa088b9e67bee9fd75554c6", features = ["serde"] }
hecs = { version = "0.10.5", features = ["serde"] }
rhai = { version = "1.17.1", features = ["serde"] }
serde = { version = "1.0.217", features = ["derive"] }
getrandom = { version = "0.2.15", features = ["js"] }
wasm-bindgen = "0.2.100"
kira = "0.10.4"

[features]
default = []
dev = []  # Enable development mode with filesystem loading

# It's recommended to set the flag on a per-target basis:
[target.wasm32-unknown-unknown]
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']

Thanks for looking!

@flydecahedron
Copy link
Author

Did some digging and I see this popup in the console:

The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu

(anonymous) | @ | rl.js:820
-- | -- | --
  | handleError | @ | rl.js:19
  | imports.wbg.__wbg_start_e81f89e130c3c86e | @ | rl.js:819
  | $__wbg_start_e81f89e130c3c86e externref shim | @ | rl_bg.wasm:0x35cdc3
  | $web_sys::features::gen_AudioBufferSourceNode::AudioBufferSourceNode::start_with_when::hf82a996ef0042bce | @ | rl_bg.wasm:0x356284
  | $<cpal::host::webaudio::Device as cpal::traits::DeviceTrait>::build_output_stream_raw::{{closure}}::hfae096beae6b4d76 | @ | rl_bg.wasm:0x223481
  | $<dyn core::ops::function::FnMut<()>+Output = R as wasm_bindgen::closure::WasmClosure>::describe::invoke::h2bd9a1d0ef90b432 | @ | rl_bg.wasm:0x358bc8
  | __wbg_adapter_33 | @ | rl.js:237
  | real | @ | rl.js:148
  | setTimeout |   |  
  | (anonymous) | @ | rl.js:767
  | handleError | @ | rl.js:19
  | imports.wbg.__wbg_setTimeout_f2fe5af8e3debeb3 | @ | rl.js:766
  | $__wbg_setTimeout_f2fe5af8e3debeb3 externref shim | @ | rl_bg.wasm:0x35c239
  | $web_sys::features::gen_Window::Window::set_timeout_with_callback_and_timeout_and_arguments_0::h7c7e358cde794e51 | @ | rl_bg.wasm:0x35218f
  | $<cpal::host::webaudio::Stream as cpal::traits::StreamTrait>::play::h966747f3d0d7e3d6 | @ | rl_bg.wasm:0x2aa680
  | $<cpal::platform::platform_impl::Stream as cpal::traits::StreamTrait>::play::h5e8a7a9a0873e433 | @ | rl_bg.wasm:0x35cf7d
  | $<kira::backend::cpal::wasm::CpalBackend as kira::backend::Backend>::start::h8e466acaa6ae0981 | @ | rl_bg.wasm:0x1b5b47
  | $kira::manager::AudioManager<B>::new::h1c0708a70684639e | @ | rl_bg.wasm:0x195601
  | $rl::audio::Audio::new::hb8cf2e1f19d24dd8 | @ | rl_bg.wasm:0x315f0a
  | $rl::main::h42694302273c45bd | @ | rl_bg.wasm:0xb4e55
  | $wasm_main | @ | rl_bg.wasm:0x34f47a
  | $__wbindgen_start | @ | rl_bg.wasm:0x35d3b9
  | __wbg_finalize_init | @ | rl.js:985
  | __wbg_init | @ | rl.js:1039
  | await in __wbg_init

Then I found this issue about for the bevy_kira_audio plugin which describes a script to fix this:
NiklasEi/bevy_kira_audio#83

So then I added that sound.js script in the bevy_game_template like so:

<!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.

@tesselode
Copy link
Owner

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?

@flydecahedron
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants