forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_control.rs
86 lines (72 loc) · 2.21 KB
/
audio_control.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! This example illustrates how to load and play an audio file, and control how it's played.
use bevy::{math::ops, prelude::*};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (update_speed, pause, mute, volume))
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
AudioPlayer::new(asset_server.load("sounds/Windless Slopes.ogg")),
MyMusic,
));
// example instructions
commands.spawn((
Text::new("-/=: Volume Down/Up\nSpace: Toggle Playback\nM: Toggle Mute"),
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(12.0),
left: Val::Px(12.0),
..default()
},
));
// camera
commands.spawn(Camera3d::default());
}
#[derive(Component)]
struct MyMusic;
fn update_speed(music_controller: Query<&AudioSink, With<MyMusic>>, time: Res<Time>) {
let Ok(sink) = music_controller.get_single() else {
return;
};
sink.set_speed((ops::sin(time.elapsed_secs() / 5.0) + 1.0).max(0.1));
}
fn pause(
keyboard_input: Res<ButtonInput<KeyCode>>,
music_controller: Query<&AudioSink, With<MyMusic>>,
) {
let Ok(sink) = music_controller.get_single() else {
return;
};
if keyboard_input.just_pressed(KeyCode::Space) {
sink.toggle_playback();
}
}
fn mute(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut music_controller: Query<&mut AudioSink, With<MyMusic>>,
) {
let Ok(mut sink) = music_controller.get_single_mut() else {
return;
};
if keyboard_input.just_pressed(KeyCode::KeyM) {
sink.toggle_mute();
}
}
fn volume(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut music_controller: Query<&mut AudioSink, With<MyMusic>>,
) {
let Ok(mut sink) = music_controller.get_single_mut() else {
return;
};
if keyboard_input.just_pressed(KeyCode::Equal) {
let current_volume = sink.volume();
sink.set_volume(current_volume + 0.1);
} else if keyboard_input.just_pressed(KeyCode::Minus) {
let current_volume = sink.volume();
sink.set_volume(current_volume - 0.1);
}
}