-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Audio control - play, pause, volume, speed, loop (#3948)
# Objective - Add ways to control how audio is played ## Solution - playing a sound will return a (weak) handle to an asset that can be used to control playback - if the asset is dropped, it will detach the sink (same behaviour as now)
- Loading branch information
Showing
6 changed files
with
258 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use bevy::{audio::AudioSink, prelude::*}; | ||
|
||
/// This example illustrates how to load and play an audio file, and control how it's played | ||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.add_system(update_speed) | ||
.add_system(pause) | ||
.add_system(volume) | ||
.run(); | ||
} | ||
|
||
fn setup( | ||
mut commands: Commands, | ||
asset_server: Res<AssetServer>, | ||
audio: Res<Audio>, | ||
audio_sinks: Res<Assets<AudioSink>>, | ||
) { | ||
let music = asset_server.load("sounds/Windless Slopes.ogg"); | ||
let handle = audio_sinks.get_handle(audio.play(music)); | ||
commands.insert_resource(MusicController(handle)); | ||
} | ||
|
||
struct MusicController(Handle<AudioSink>); | ||
|
||
fn update_speed( | ||
audio_sinks: Res<Assets<AudioSink>>, | ||
music_controller: Res<MusicController>, | ||
time: Res<Time>, | ||
) { | ||
if let Some(sink) = audio_sinks.get(&music_controller.0) { | ||
sink.set_speed(((time.seconds_since_startup() / 5.0).sin() as f32 + 1.0).max(0.1)); | ||
} | ||
} | ||
|
||
fn pause( | ||
keyboard_input: Res<Input<KeyCode>>, | ||
audio_sinks: Res<Assets<AudioSink>>, | ||
music_controller: Res<MusicController>, | ||
) { | ||
if keyboard_input.just_pressed(KeyCode::Space) { | ||
if let Some(sink) = audio_sinks.get(&music_controller.0) { | ||
if sink.is_paused() { | ||
sink.play() | ||
} else { | ||
sink.pause() | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn volume( | ||
keyboard_input: Res<Input<KeyCode>>, | ||
audio_sinks: Res<Assets<AudioSink>>, | ||
music_controller: Res<MusicController>, | ||
) { | ||
if let Some(sink) = audio_sinks.get(&music_controller.0) { | ||
if keyboard_input.just_pressed(KeyCode::Plus) { | ||
sink.set_volume(sink.volume() + 0.1); | ||
} else if keyboard_input.just_pressed(KeyCode::Minus) { | ||
sink.set_volume(sink.volume() - 0.1); | ||
} | ||
} | ||
} |