rodio
rodio copied to clipboard
Could use an example for absolute rust newbies (like myself) on how to use an include_bytes! macro with this library.
//audio
// Get a output stream handle to the default physical sound device
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
// Decode that sound file into a source
let my_slice = std::io::Cursor::new(include_bytes!("../title.mp3").as_ref());
let source = Decoder::new(my_slice).unwrap();
// Play the sound directly on the device
let _sound_result = stream_handle.play_raw(source.convert_samples());
Thanks so much for at least posting this! Was about to implement Seek myself for a slice of bytes, because I couldn't find the appropriate std library struct wrapper 😅
PRs welcome!
And if you are using tokio/async, this example looks like:
async fn emit_sound() {
tokio::task::spawn_blocking(|| {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
let my_slice = std::io::Cursor::new(include_bytes!("../assets/click.mp3").as_ref());
let source = Decoder::new(my_slice).unwrap();
sink.append(source);
sink.sleep_until_end();
})
.await
.unwrap();
}