forked from NiklasEi/bevy_kira_audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.rs
106 lines (100 loc) · 3.9 KB
/
status.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use bevy::prelude::*;
use bevy_kira_audio::{Audio, AudioPlugin, InstanceHandle};
struct LoopAudioInstanceHandle {
instance_handle: InstanceHandle,
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(AudioPlugin)
.add_startup_system(start_audio)
.add_startup_system(display_help_text)
.add_system(print_status)
.add_system(process_keyboard_input)
.run();
}
fn start_audio(mut commands: Commands, asset_server: Res<AssetServer>, audio: Res<Audio>) {
let asset_handle = asset_server.load("sounds/loop.ogg");
let instance_handle = audio.play_looped(asset_handle);
println!("Audio started.");
commands.insert_resource(LoopAudioInstanceHandle { instance_handle });
}
fn process_keyboard_input(audio: Res<Audio>, kb: Res<Input<KeyCode>>) {
if kb.just_pressed(KeyCode::P) {
audio.pause();
println!("Audio paused.");
} else if kb.just_pressed(KeyCode::S) {
audio.stop();
println!("Audio stopped.");
} else if kb.just_pressed(KeyCode::R) {
audio.resume();
println!("Audio resumed.");
}
}
fn print_status(audio: Res<Audio>, loop_audio: Res<LoopAudioInstanceHandle>) {
let state = audio.state(loop_audio.instance_handle.clone());
println!("Loop audio {:?}", state);
}
fn display_help_text(mut commands: Commands, asset_server: Res<AssetServer>) {
let monogram = asset_server.load("fonts/monogram.ttf");
commands.spawn_bundle(UiCameraBundle::default());
commands
.spawn_bundle(NodeBundle {
style: Style {
size: Size {
width: Val::Percent(100.),
height: Val::Percent(100.),
},
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
color: Color::rgba(0., 0., 0., 0.).into(),
..Default::default()
})
.with_children(|parent| {
parent.spawn_bundle(TextBundle {
text: Text {
sections: vec![
TextSection {
value: "Press 'P' to pause\n".to_string(),
style: TextStyle {
font: monogram.clone(),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
},
TextSection {
value: "Press 'R' to resume\n".to_string(),
style: TextStyle {
font: monogram.clone(),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
},
TextSection {
value: "Press 'S' to stop\n\n".to_string(),
style: TextStyle {
font: monogram.clone(),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
},
TextSection {
value: "Check your console for the audio state".to_string(),
style: TextStyle {
font: monogram.clone(),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
},
],
alignment: TextAlignment {
vertical: VerticalAlign::Center,
horizontal: HorizontalAlign::Center,
},
},
..Default::default()
});
});
}