feat(stream): add a demo that actually streams and plays music from my jellyfin server

This commit is contained in:
Christoph J. Scherr 2024-08-24 01:23:13 +02:00
parent 6a5cbcdbea
commit 5dbc2ac6d2
2 changed files with 40 additions and 0 deletions

View File

@ -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"] }

View File

@ -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<dyn Error>> {
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(())
}