From 5dbc2ac6d20db613a3d7d1e7adeea0334229fa2c Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 24 Aug 2024 01:23:13 +0200 Subject: [PATCH] feat(stream): add a demo that actually streams and plays music from my jellyfin server --- demo/try_stream/Cargo.toml | 9 +++++++++ demo/try_stream/src/main.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 demo/try_stream/Cargo.toml create mode 100644 demo/try_stream/src/main.rs diff --git a/demo/try_stream/Cargo.toml b/demo/try_stream/Cargo.toml new file mode 100644 index 0000000..cf4a034 --- /dev/null +++ b/demo/try_stream/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "try_stream" +version = "0.1.0" +edition = "2021" + +[dependencies] +reqwest = "0.12.7" +rodio = { version = "0.19.0", features = ["symphonia-all"] } +tokio = { version = "1.39.3", features = ["rt", "macros"] } diff --git a/demo/try_stream/src/main.rs b/demo/try_stream/src/main.rs new file mode 100644 index 0000000..502121a --- /dev/null +++ b/demo/try_stream/src/main.rs @@ -0,0 +1,31 @@ +use std::error::Error; +use std::result::Result; + +use rodio::{Decoder, OutputStream, Source}; + +#[tokio::main(flavor = "current_thread")] +async fn main() -> Result<(), Box> { + let client = reqwest::Client::new(); + let url = "https://jellyfin.homeserver.box/Items/758d55bf6b122523c9bed78fe8893071/Download?api_key=redacted"; + + let res = client + .get(url) + .header("User-Agent", "My Rust Program/1.0") + .send() + .await?; + + let audio_stream = std::io::Cursor::new(res.bytes().await.unwrap()); + + // Get an 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 source = Decoder::new(audio_stream).unwrap(); + // Play the sound directly on the device + stream_handle.play_raw(source.convert_samples()).unwrap(); + + // The sound plays in a separate audio thread, + // so we need to keep the main thread alive while it's playing. + std::thread::sleep(std::time::Duration::from_secs(5)); + + Ok(()) +}