rodio
rodio copied to clipboard
Example on docs.rs doesn't work
I am trying to get to know about this library, it looks pretty nice to me as it provides a highly abstracted interface that no other library does; however the example provided was not working, both on Windows 10, Linux (Ubuntu 18.04) and macOS (10.14.3). Program exited as soon as it starts, no panic, error messages, etc. But also no sound.
use std::fs::File;
use std::io::BufReader;
use rodio::Source;
let device = rodio::default_output_device().unwrap();
let file = File::open("sound.ogg").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
rodio::play_raw(&device, source.convert_samples());
I've tried to use different audio source files and have also converted them into different formats (wav, mp3, ogg, flac), but none of those helped.
Edit: also tried to connect to different output devices (speaker, headphones, USB DAC), but still no luck.
My guess is that your main thread is exiting immediately and - as rodio plays audio in a background thread - the program ends before it gets to play anything. Try adding a loop {} or similar after the last line.
Thanks, or adding:
use std::thread::sleep;
[...]
sleep(Duration::from_millis(10000)); // Sleep for ten seconds.
... does the trick too ;)