Skip to content
Discussion options

You must be logged in to vote

Playing an audio clip in Bevy 0.11 is as easy as spawning an AudioBundle.

Here's some code similar to the YouTube video:

fn play_sound(mut commands: Commands, asset_server: Res<AssetServer>) {
    let sound = if random::<f32>() > 0.5 {
        asset_server.load("one.ogg")
    } else {
        asset_server.load("two.ogg")
    };

    commands.spawn(AudioBundle {
        source: sound,
        // auto-despawn the entity when playback finishes
        settings: PlaybackSettings::DESPAWN,
    });
}

There is one slight problem with this code: The audio files may be unloaded and their handles cleaned up by the asset server. This won't prevent things from working, but your app might have to do e…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@aligator
Comment options

Answer selected by aligator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants