rodio icon indicating copy to clipboard operation
rodio copied to clipboard

Example on docs.rs doesn't work

Open po-stulate opened this issue 6 years ago • 2 comments

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.

po-stulate avatar May 16 '19 19:05 po-stulate

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.

silverweed avatar May 21 '19 15:05 silverweed

Thanks, or adding:

use std::thread::sleep;
[...]
sleep(Duration::from_millis(10000)); // Sleep for ten seconds.

... does the trick too ;)

benmarten avatar Feb 27 '20 04:02 benmarten