Trait bevy::prelude::AudioSinkPlayback

pub trait AudioSinkPlayback {
    // Required methods
    fn volume(&self) -> f32;
    fn set_volume(&self, volume: f32);
    fn speed(&self) -> f32;
    fn set_speed(&self, speed: f32);
    fn play(&self);
    fn pause(&self);
    fn is_paused(&self) -> bool;
    fn stop(&self);
    fn empty(&self) -> bool;

    // Provided method
    fn toggle(&self) { ... }
}
Expand description

Common interactions with an audio sink.

Required Methods§

fn volume(&self) -> f32

Gets the volume of the sound.

The value 1.0 is the “normal” volume (unfiltered input). Any value other than 1.0 will multiply each sample by this value.

fn set_volume(&self, volume: f32)

Changes the volume of the sound.

The value 1.0 is the “normal” volume (unfiltered input). Any value other than 1.0 will multiply each sample by this value.

§Note on Audio Volume

An increase of 10 decibels (dB) roughly corresponds to the perceived volume doubling in intensity. As this function scales not the volume but the amplitude, a conversion might be necessary. For example, to halve the perceived volume you need to decrease the volume by 10 dB. This corresponds to 20log(x) = -10dB, solving x = 10^(-10/20) = 0.316. Multiply the current volume by 0.316 to halve the perceived volume.

fn speed(&self) -> f32

Gets the speed of the sound.

The value 1.0 is the “normal” speed (unfiltered input). Any value other than 1.0 will change the play speed of the sound.

fn set_speed(&self, speed: f32)

Changes the speed of the sound.

The value 1.0 is the “normal” speed (unfiltered input). Any value other than 1.0 will change the play speed of the sound.

fn play(&self)

Resumes playback of a paused sink.

No effect if not paused.

fn pause(&self)

Pauses playback of this sink.

No effect if already paused. A paused sink can be resumed with play.

fn is_paused(&self) -> bool

Is this sink paused?

Sinks can be paused and resumed using pause and play.

fn stop(&self)

Stops the sink.

It won’t be possible to restart it afterwards.

fn empty(&self) -> bool

Returns true if this sink has no more sounds to play.

Provided Methods§

fn toggle(&self)

Toggles the playback of this sink.

Will pause if playing, and will be resumed if paused.

Examples found in repository?
examples/audio/audio_control.rs (line 38)
32
33
34
35
36
37
38
39
40
41
fn pause(
    keyboard_input: Res<ButtonInput<KeyCode>>,
    music_controller: Query<&AudioSink, With<MyMusic>>,
) {
    if keyboard_input.just_pressed(KeyCode::Space) {
        if let Ok(sink) = music_controller.get_single() {
            sink.toggle();
        }
    }
}

Implementors§