pub struct Audio { /* private fields */ }
Expand description
A simple 4-track audio system to load/decode audio files from disk to play later. Supported formats are: MP3, WAV, Vorbis and Flac.
Implementations§
Source§impl Audio
impl Audio
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new sound subsystem. You only need one of these – you can use it to load and play any number of audio clips.
Examples found in repository?
3fn main() {
4 // Create a 4-channel audio subsystem.
5 let mut audio = Audio::new();
6 // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7 // but at least doesn't crash. Helpful for CI.
8 if audio.disabled() {
9 println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10 }
11 audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12 audio.play("startup"); // Execution continues while playback occurs in another thread.
13 audio.wait(); // Block until sounds finish playing
14}
Sourcepub fn disabled(&self) -> bool
pub fn disabled(&self) -> bool
If no sound device was detected, the audio subsystem will run in a disabled mode that doesn’t actually do anything. This method indicates whether audio is disabled.
Examples found in repository?
3fn main() {
4 // Create a 4-channel audio subsystem.
5 let mut audio = Audio::new();
6 // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7 // but at least doesn't crash. Helpful for CI.
8 if audio.disabled() {
9 println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10 }
11 audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12 audio.play("startup"); // Execution continues while playback occurs in another thread.
13 audio.wait(); // Block until sounds finish playing
14}
Sourcepub fn add<S: AsRef<str>, P: AsRef<Path>>(&mut self, name: S, path: P)
pub fn add<S: AsRef<str>, P: AsRef<Path>>(&mut self, name: S, path: P)
Add an audio clip to play. Audio clips will be decoded and buffered during this call so
the first call to .play()
is not staticky if you compile in debug mode. name
is what
you will refer to this clip as when you need to play it. Files known to be supported by the
underlying library (rodio) at the time of this writing are MP3, WAV, Vorbis and Flac.
Examples found in repository?
3fn main() {
4 // Create a 4-channel audio subsystem.
5 let mut audio = Audio::new();
6 // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7 // but at least doesn't crash. Helpful for CI.
8 if audio.disabled() {
9 println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10 }
11 audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12 audio.play("startup"); // Execution continues while playback occurs in another thread.
13 audio.wait(); // Block until sounds finish playing
14}
Sourcepub fn play<S: AsRef<str>>(&mut self, name: S)
pub fn play<S: AsRef<str>>(&mut self, name: S)
Play an audio clip that has already been loaded. name
is the name you chose when you
added the clip to the Audio
system. If you forgot to load the clip first, this will crash.
Examples found in repository?
3fn main() {
4 // Create a 4-channel audio subsystem.
5 let mut audio = Audio::new();
6 // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7 // but at least doesn't crash. Helpful for CI.
8 if audio.disabled() {
9 println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10 }
11 audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12 audio.play("startup"); // Execution continues while playback occurs in another thread.
13 audio.wait(); // Block until sounds finish playing
14}
Sourcepub fn wait(&self)
pub fn wait(&self)
Block until no sounds are playing. Convenient for keeping a thread alive until all sounds have played.
Examples found in repository?
3fn main() {
4 // Create a 4-channel audio subsystem.
5 let mut audio = Audio::new();
6 // On systems without an audio card, Audio will run in disabled mode, which doesn't do anything,
7 // but at least doesn't crash. Helpful for CI.
8 if audio.disabled() {
9 println!("Sorry, we couldn't find an audio device, so no sound will play for you. :'-(");
10 }
11 audio.add("startup", "audio_subsystem_initialized.mp3"); // Load the sound, give it a name
12 audio.play("startup"); // Execution continues while playback occurs in another thread.
13 audio.wait(); // Block until sounds finish playing
14}