#Unable to Play Audio Files

7 messages · Page 1 of 1 (latest)

wheat gazelle
#

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?

wheat gazelle
#

Unable to Play Audio Files

fluid imp
#

did your perhaps disable default features in bevy? ogg files should load file otherwise as OGG support is added by vorbis which is a default feature

wheat gazelle
fluid imp
#

hm, that's odd. audio is played as soon as the asset is fully loaded. for it to not play anything either the files don't exist in the root of the assets folder (it will silently fail) or your timer logic is somehow wrong and not actually spawning anythinf or the system wasn't added to the App?

wheat gazelle
fluid imp
#

ah, indeed. that's a known issue that hasn't been fixed yet it seems. you need to disable asset meta checking on the asset plugin, like this:

app.add_plugins(DefaultPlugins.set(AssetPlugin {
        meta_check: AssetMetaCheck::Never,
        ..default()
    }));