Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: playing audio from an async stream #439

Closed
MikMuellerDev opened this issue Sep 4, 2022 · 3 comments
Closed

Question: playing audio from an async stream #439

MikMuellerDev opened this issue Sep 4, 2022 · 3 comments

Comments

@MikMuellerDev
Copy link

MikMuellerDev commented Sep 4, 2022

Hello, I like rodio but am struggeling to play audio from a network stream.

Problem

I have a stream of type futures_util::StreamExt which has been fetched using the reqwest crate.
The problem is that this stream does not support Seek and other traits required by rodio.
I tried buffering the stream, then playing it, but it leads to pauses during playing.
This is a live-stream so i cannot download the entire response but must play it in an iterator-like fashion.
I do not know how to convert this stream into a type which implements the required traits used by rodio.

Here is what this stream looks like:

let mut stream = reqwest::get(url).await.unwrap().bytes_stream();

The optimal solution would transform the stream and play it directly.

@JasonWei512
Copy link

JasonWei512 commented Nov 14, 2022

I managed to stream network mp3:

https://github.com/JasonWei512/code-radio-cli

See mp3_stream_decoder.rs and player.rs.

Basically, you need to:

  1. Copy https://github.com/RustAudio/rodio/blob/master/src/decoder/mp3.rs, and remove the Seek trait bound

  2. Use blocking reqwest:

let response = reqwest::blocking::get(listen_url).unwrap();
let source = Mp3StreamDecoder::new(response).unwrap();  // The Mp3Decoder without `Seek` trait bound in step 1
let sink = rodio::Sink::try_new(&stream_handle).unwrap();
sink.append(source);

@MikMuellerDev
Copy link
Author

Okay, thank you very much, I will soon give this a try :)

@MikMuellerDev
Copy link
Author

Despite this issue being closed, i would like to thank you for your answer.
I too managed to get a successfully working program using your solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants