Skip to content

No audio in WASM build #124

@flydecahedron

Description

@flydecahedron

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions