-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Closed
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5ac2cd3
add audio controls
mockersf 04afb00
and an example
mockersf 84947c7
doc and comments
mockersf 2778876
add volume in example
mockersf 2e87fa0
use a single struct instead of a tuple in the queue
mockersf 3b4d4bb
doc
mockersf 1ce4a21
typlo
mockersf 563fc59
comment about system execution
mockersf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
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) | ||
.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(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() | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn volume( | ||
keyboard_input: Res<Input<KeyCode>>, | ||
audio_sinks: Res<Assets<AudioSink>>, | ||
music_controler: Res<MusicControler>, | ||
) { | ||
if let Some(sink) = audio_sinks.get(&music_controler.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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me this looks like a manual impl of Debug that should be identical to the derive.
Am i missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the automatic derive needs
Source
to beDebug
, but not a manual impl... I'm not quite sure the debug impl is actually useful though, I added it to keep existing functionality