Crate bevy_kira_components

source ·
Expand description

Add audio support to Bevy through the [kira] crate.

This crate aims at creating a replacement for bevy_audio by instead integrating Kira, a crate for audio playback aimed at games, and used in several other Rust projects.

This particular crate is an experiment in making a component-based ECS API, instead of a resource-based approach, currently taken by bevy_kira_audio.

To get started playing sounds, insert an AudioBundle on an entity. This is a generic bundle which supports any compatible sound source. An implementation over audio files is provided, with streaming support, using the AudioFileBundle type alias.

When an AudioFile is inserted, its playback will begin right away. To prevent that, use the start_paused field from AudioFileBundle::settings and set it to false.

The audio system creates an AudioHandle component when registering an added sound for playback. This handle allows you to control the sound. For AudioFiles, this means pausing/resuming, setting the volume, panning, and playback rate of the sound.

Spatial audio is supported built-in with the SpatialEmitter component, which tells the plugin to add the entity as an emitter, provided it also has a [GlobalTransform] component attached. Its settings control the behavior of the spatial effect.

§Example

use bevy::prelude::*;
use bevy_kira_components::prelude::*;

fn main() {
    App::new()
        .insert_non_send_resource(AudioSettings {
            // Only needed for tests
            backend_settings: AudioBackendSelector::Mock { sample_rate: 48000, },
            ..default()
        })
        .add_plugins((DefaultPlugins, AudioPlugin))
        .add_systems(Startup, add_sound)
        .run();
}

fn add_sound(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(AudioFileBundle {
        source: asset_server.load("my_sound.ogg"),
        ..default()
    });
}

Re-exports§

  • pub use kira;

Modules§

  • Register diagnostics regarding statistics of Kira’s audio engine usage.
  • Implementations of different audio sources.
  • Support for spatial audio through kira’s spatial features.

Structs§

  • Adds audio to Bevy games via the [kira] crate.
  • System set used in grouping systems that setup audio sources. Used in the AudioSourcePlugin’s systems. Useful to place systems right after to be able to react to added audio assets.
  • Main resource holding all the bookkeeping necessary to link the ECS data to the audio engine.

Enums§

Type Aliases§

  • Type of settings for the audio engine. Insert it as a resource before adding the plugin to change the default settings.