Home

Play An Audio File In Rust

rust
```cargo
[dependencies]
rodio = "0.17"
```
use std::fs::File;
use std::io::BufReader;
use rodio::{Decoder, OutputStream, Sink};

fn main() { 
  println!("playing sound");
  let (_stream, stream_handle) = OutputStream::try_default().unwrap();
  let file = BufReader::new(File::open("audio/hydrate.mp3").unwrap());
  let sink = Sink::try_new(&stream_handle).unwrap();
  let source = Decoder::new(file).unwrap();
  sink.append(source);
  sink.sleep_until_end();
}
results start
~ fin ~

References