-
Notifications
You must be signed in to change notification settings - Fork 54
/
channel_control.rs
42 lines (38 loc) · 1.36 KB
/
channel_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
use bevy::prelude::*;
use bevy_kira_audio::prelude::*;
use kira::tween::Easing;
use std::time::Duration;
// This example demonstrates how to control an audio channel
// This kind of control is deferred to the end of the current frame update
// Left-click to pause the audio
// Right-click to resume the audio
fn main() {
App::new()
.add_plugins((DefaultPlugins, AudioPlugin))
.add_systems(Startup, play_loop)
.add_systems(Update, channel_control)
.run();
}
fn channel_control(input: Res<ButtonInput<MouseButton>>, audio: Res<Audio>) {
if input.just_pressed(MouseButton::Left) {
audio
.pause()
.fade_out(AudioTween::new(Duration::from_secs(2), Easing::Linear));
} else if input.just_pressed(MouseButton::Right) {
audio
.resume()
.fade_in(AudioTween::new(Duration::from_secs(2), Easing::Linear));
}
}
fn play_loop(mut commands: Commands, asset_server: Res<AssetServer>, audio: Res<Audio>) {
audio.play(asset_server.load("sounds/loop.ogg")).looped();
audio.play(asset_server.load("sounds/sound.ogg"));
commands.spawn(Camera2d);
commands.spawn(Text::new(
r#"
This example demonstrates how to control an audio channel
The audio commands have 2s linear fade in/out
Left-click to pause the audio
Right-click to resume the audio"#,
));
}