pub fn open_file<P: AsRef<Path>>(path: P) -> Result<Box<dyn Sound>, Error>
Expand description
Create a Sound that reads from a file with the correct decoder based on the file extension.
If the file type is not able to be decoded than an [ErrorKind::Unsupported] is returned.
Uses a BufReader internally with the default capacity.
The returned Sound reads using File. This is generally not recommended on the renderer thread as reading from a file could block the renderer. Consider convert the sound to a memory_sound which is stored entirely in RAM (and can be cloned cheaply).
Examples found in repository?
examples/play.rs (line 10)
3 4 5 6 7 8 9 10 11 12 13 14 15 16
fn main() -> Result<(), Box<dyn std::error::Error>> {
let Some(file_path) = args() else {
eprintln!("usage: FILE_PATH");
std::process::exit(2);
};
let (mut manager, _backend) = awedio::start()?;
let (sound, notifier) = awedio::sounds::open_file(file_path)?.with_completion_notifier();
manager.play(Box::new(sound));
let _ = notifier.recv();
Ok(())
}
More examples
examples/count_samples.rs (line 9)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
let Some(file_path) = args() else {
eprintln!("usage: FILE_PATH");
std::process::exit(2);
};
let mut sound = awedio::sounds::open_file(file_path)?;
let mut num_samples = 0;
loop {
match sound.next_sample() {
Ok(NextSample::Sample(_)) => num_samples += 1,
Ok(NextSample::Paused) => {
println!("Encountered a Pause. Stopping.");
break;
}
Ok(NextSample::Finished) => {
break;
}
Ok(NextSample::MetadataChanged) => {
println!(
"Encountered MetadataChanged. New sample rate: {}, New channel count: {}",
sound.sample_rate(),
sound.channel_count()
);
}
Err(e) => {
println!("Encountered error: {:?}", e);
break;
}
}
}
println!("Read {} samples.", num_samples);
Ok(())
}