pub trait AudioControl {
// Required methods
fn play(&self, audio_source: Handle<AudioSource>) -> PlayAudioCommand<'_>;
fn stop(&self) -> TweenCommand<'_, FadeOut>;
fn pause(&self) -> TweenCommand<'_, FadeOut>;
fn resume(&self) -> TweenCommand<'_, FadeIn>;
fn set_volume(&self, volume: impl Into<Volume>) -> TweenCommand<'_, FadeIn>;
fn set_panning(&self, panning: f64) -> TweenCommand<'_, FadeIn>;
fn set_playback_rate(&self, playback_rate: f64) -> TweenCommand<'_, FadeIn>;
fn state(&self, instance_handle: &Handle<AudioInstance>) -> PlaybackState;
fn is_playing_sound(&self) -> bool;
}
Expand description
Play and control audio
Required Methods§
Sourcefn play(&self, audio_source: Handle<AudioSource>) -> PlayAudioCommand<'_>
fn play(&self, audio_source: Handle<AudioSource>) -> PlayAudioCommand<'_>
Play audio
fn my_system(asset_server: Res<AssetServer>, audio: Res<Audio>) {
audio.play(asset_server.load("audio.mp3"));
}
Sourcefn stop(&self) -> TweenCommand<'_, FadeOut>
fn stop(&self) -> TweenCommand<'_, FadeOut>
Stop all audio
fn my_system(audio: Res<Audio>) {
audio.stop();
}
Sourcefn pause(&self) -> TweenCommand<'_, FadeOut>
fn pause(&self) -> TweenCommand<'_, FadeOut>
Pause all audio
fn my_system(audio: Res<Audio>) {
audio.pause();
}
Sourcefn resume(&self) -> TweenCommand<'_, FadeIn>
fn resume(&self) -> TweenCommand<'_, FadeIn>
Resume all audio
fn my_system(audio: Res<Audio>) {
audio.resume();
}
Sourcefn set_volume(&self, volume: impl Into<Volume>) -> TweenCommand<'_, FadeIn>
fn set_volume(&self, volume: impl Into<Volume>) -> TweenCommand<'_, FadeIn>
Set the volume
The default value is 1. This method supports setting the volume in Decibels or as Amplitude.
fn my_system(audio: Res<Audio>) {
audio.set_volume(0.5);
}
Sourcefn set_panning(&self, panning: f64) -> TweenCommand<'_, FadeIn>
fn set_panning(&self, panning: f64) -> TweenCommand<'_, FadeIn>
Set panning
The default value is 0.5 Values up to 1 pan to the right Values down to 0 pan to the left
fn my_system(audio: Res<Audio>) {
audio.set_panning(0.9);
}
Sourcefn set_playback_rate(&self, playback_rate: f64) -> TweenCommand<'_, FadeIn>
fn set_playback_rate(&self, playback_rate: f64) -> TweenCommand<'_, FadeIn>
Set playback rate
The default value is 1
fn my_system(audio: Res<Audio>) {
audio.set_playback_rate(2.0);
}
Sourcefn state(&self, instance_handle: &Handle<AudioInstance>) -> PlaybackState
fn state(&self, instance_handle: &Handle<AudioInstance>) -> PlaybackState
Get state for a playback instance.
Sourcefn is_playing_sound(&self) -> bool
fn is_playing_sound(&self) -> bool
Returns true
if there is any sound in this channel that is in the state Playing
, Pausing
, or Stopping
If there are only Stopped
, Paused
, or Queued
sounds, the method will return false
.
The same result is returned if there are no sounds in the channel at all.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.