Struct ears::Sound
[−]
[src]
pub struct Sound { // some fields omitted }
Play Sounds easily.
Simple class to play sounds easily in 2 lines. Sounds are really light objects, the sound's data is entirely loaded into memory and can be shared between Sounds using the SoundData object.
Examples
extern crate ears; use ears::{Sound, AudioController}; fn main() -> () { // Create a Sound with the path of the sound file. let mut snd = Sound::new("path/to/my/sound.ogg").unwrap(); // Play it snd.play(); // Wait until the sound stopped playing while snd.is_playing() {} }
Methods
impl Sound
fn new(path: &str) -> Option<Sound>
Default constructor for Sound struct.
Create a new struct and an associated SoundData.
Argument
path
- The path of the sound file to create the SoundData.
Return
An Option with Some(Sound) if the Sound is created properly, or None if un error has occured.
Example
let snd = match ears::Sound::new("path/to/the/sound.ogg") { Some(snd) => snd, None => panic!("Cannot load the sound from a file !") };
fn new_with_data(sound_data: Rc<RefCell<SoundData>>) -> Option<Sound>
Create a new struct with a SoundData to associate.
Argument
sound_data
- The sound_data to associate to the Sound.
Return
An Option with Some(Sound) if the Sound is created properly, or None if un error has occured.
Example
use ears::{Sound, SoundData, AudioController}; use std::rc::Rc; use std::cell::RefCell; let snd_data = match SoundData::new("path/to/the/sound.ogg") { Some(snd_data) => Rc::new(RefCell::new(snd_data)), None => panic!("Cannot create the sound data !") }; let mut snd = match Sound::new_with_data(snd_data) { Some(mut snd) => snd.play(), None => panic!("Cannot create a sound using a sound data !") };
fn get_datas(&self) -> Rc<RefCell<SoundData>>
Get the sound datas.
Return
The SoundData associated to this Sound.
Example
let snd = ears::Sound::new("path/to/the/sound.ogg").unwrap(); let snd_data = snd.get_datas();
fn set_datas(&mut self, sound_data: Rc<RefCell<SoundData>>)
Set the sound datas.
Doesn't work if the sound is currently playing.
Argument
sound_data
- The new sound_data
Example
let snd1 = ears::Sound::new("path/to/the/sound.ogg").unwrap(); let mut snd2 = ears::Sound::new("other/path/to/the/sound.ogg").unwrap(); let snd_data = snd1.get_datas(); snd2.set_datas(snd_data);