forked from Salzian/bevy_fmod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimal.rs
More file actions
46 lines (39 loc) · 1.69 KB
/
minimal.rs
File metadata and controls
46 lines (39 loc) · 1.69 KB
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
//! This example demonstrates how to use the FmodPlugin to play a sound.
//! Make sure to follow the instructions in the README.md to set up the demo project.
use bevy::prelude::*;
use bevy_fmod::prelude::AudioSource;
use bevy_fmod::prelude::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
// Add your audio banks here to the plugin. Make sure to include at
// least the master bank and it's strings bank.
FmodPlugin::new(&[
"./assets/audio/demo_project/Build/Desktop/Master.bank",
"./assets/audio/demo_project/Build/Desktop/Master.strings.bank",
"./assets/audio/demo_project/Build/Desktop/Music.bank",
]),
))
.add_systems(Startup, startup)
.add_systems(PostStartup, play_music)
.run();
}
#[derive(Component)]
struct MyMusicPlayer;
fn startup(mut commands: Commands, studio: Res<FmodStudio>) {
// To play a sound, you need to create an event instance.
// To create an event instance, you need to get the event description.
let event_description = studio.get_event("event:/Music/Level 03").unwrap();
// With the event description, you can create the event instance.
let event_instance = event_description.create_instance().unwrap();
// To place the event instance in the world, you need to spawn a AudioSource component.
commands.spawn(MyMusicPlayer).insert(AudioSource {
event_instance: event_instance,
despawn_stop_mode: StopMode::AllowFadeout,
});
}
fn play_music(audio_source: Single<&AudioSource, With<MyMusicPlayer>>) {
// To play the event instance, you need to start it.
audio_source.start().unwrap();
}