rodio icon indicating copy to clipboard operation
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.

Open Roxfall opened this issue 4 years ago • 3 comments

//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());

Roxfall avatar Jan 12 '22 01:01 Roxfall

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 😅

austintheriot avatar Jun 12 '22 20:06 austintheriot

PRs welcome!

est31 avatar Jun 12 '22 21:06 est31

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();
}

srid avatar Oct 27 '23 19:10 srid