Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Play An Audio File In Rust

Code

```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

playing sound

References