Skip to content

Instantly share code, notes, and snippets.

@madskjeldgaard
Last active December 22, 2022 15:00
Show Gist options
  • Save madskjeldgaard/dabe89a2564c2b02c83375ba2fdc891e to your computer and use it in GitHub Desktop.
Save madskjeldgaard/dabe89a2564c2b02c83375ba2fdc891e to your computer and use it in GitHub Desktop.
Simple buffer playback in Rust using the rodio crate
use rodio::Sink;
use std::fs::File;
use std::io::BufReader;
fn main() {
// Sound output
let device = rodio::default_output_device().unwrap();
let sink = Sink::new(&device);
// Buffer playback
let file_path: String = "/home/mads/testsound/plastic1.wav".to_string();
let file = File::open(file_path).unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
// Add sound to the sink
sink.append(source);
// Volume
sink.set_volume(0.25);
// Keep the sink alive until sound stops playing
sink.sleep_until_end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment