I'm trying to play audio files in Bevy, but nothing plays. I'm loading the audio files in the setup function and storing them as a resource:
use bevy::prelude::*;
#[derive(Resource)]
struct NoteSounds {
sounds: bevy::utils::hashbrown::HashMap<u8, Handle<AudioSource>>, // Maps note keys to audio handles
}
fn setup(asset_server: Res<AssetServer>, mut commands: Commands) {
commands.spawn(Camera2d);
commands.insert_resource(NoteSounds {
sounds: bevy::utils::hashbrown::HashMap::from([
(0, asset_server.load("bass.ogg")),
(1, asset_server.load("bd.ogg")),
(2, asset_server.load("harp.ogg")),
(3, asset_server.load("snare.ogg")),
(4, asset_server.load("hat.ogg")),
(5, asset_server.load("guitar.ogg")),
(6, asset_server.load("flute.ogg")),
(7, asset_server.load("bell.ogg")),
(8, asset_server.load("icechime.ogg")),
(9, asset_server.load("xylobone.ogg")),
(10, asset_server.load("iron_xylophone.ogg")),
(11, asset_server.load("cow_bell.ogg")),
(12, asset_server.load("didgeridoo.ogg")),
(13, asset_server.load("bit.ogg")),
(14, asset_server.load("banjo.ogg")),
(15, asset_server.load("pling.ogg")),
]),
});
}
Then, in the update function, I try to play the audio every 3 seconds:
fn update(time: Res<Time>, mut commands: Commands, asset_server: Res<NoteSounds>) {
if time.elapsed_secs() % 3.0 < time.delta_secs() {
let audio = asset_server.sounds.get(&0).unwrap();
commands.spawn(AudioPlayer::new(audio.clone()));
}
}
However, no sound plays. Am I missing something? How should I properly play audio in Bevy?
Also I plan to play many audio at the same time, is this the best way to do it?