Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Audio control - play, pause, volume, speed, loop #3948

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
and an example
  • Loading branch information
mockersf committed Feb 14, 2022
commit 04afb00d68a4ed32654e3f407fcebdaa15d45f1f
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ path = "examples/async_tasks/external_source_external_thread.rs"
name = "audio"
path = "examples/audio/audio.rs"

[[example]]
name = "audio_control"
path = "examples/audio/audio_control.rs"

# Diagnostics
[[example]]
name = "log_diagnostics"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Example | File | Description
Example | File | Description
--- | --- | ---
`audio` | [`audio/audio.rs`](./audio/audio.rs) | Shows how to load and play an audio file
`audio_control` | [`audio/audio_control.rs`](./audio/audio_control.rs) | Shows how to load and play an audio file, and control how it's played

## Diagnostics

Expand Down
51 changes: 51 additions & 0 deletions examples/audio/audio_control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use bevy::audio::AudioSink;
use bevy::prelude::*;

/// This example illustrates how to load and play an audio file, and control how it's played
mockersf marked this conversation as resolved.
Show resolved Hide resolved
fn main() {
App::new()
mockersf marked this conversation as resolved.
Show resolved Hide resolved
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(update_speed)
.add_system(pause)
.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(MusicControler(handle));
}

struct MusicControler(Handle<AudioSink>);
mockersf marked this conversation as resolved.
Show resolved Hide resolved

fn update_speed(
audio_sinks: Res<Assets<AudioSink>>,
music_controler: Res<MusicControler>,
time: Res<Time>,
) {
if let Some(sink) = audio_sinks.get(&music_controler.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_controler: Res<MusicControler>,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
if let Some(sink) = audio_sinks.get(&music_controler.0) {
if sink.is_paused() {
sink.play()
} else {
sink.pause()
}
}
}
}