我已经使用rodio机箱从本地文件播放音频,通过浏览文档,但无法找到如何使用url播放音频。
发布于 2020-08-18 10:12:27
下面是一个使用阻塞reqwest的简单示例。这将在开始播放之前将整个音频文件下载到内存中。
use std::io::{Write, Read, Cursor};
use rodio::Source;
fn main() {
// Remember to add the "blocking" feature in the Cargo.toml for reqwest
let resp = reqwest::blocking::get("http://websrvr90va.audiovideoweb.com/va90web25003/companions/Foundations%20of%20Rock/13.01.mp3")
.unwrap();
let mut cursor = Cursor::new(resp.bytes().unwrap()); // Adds Read and Seek to the bytes via Cursor
let source = rodio::Decoder::new(cursor).unwrap(); // Decoder requires it's source to impl both Read and Seek
let device = rodio::default_output_device().unwrap();
rodio::play_raw(&device, source.convert_samples()); // Plays on a different thread
loop {} // Don't exit immediately, so we can hear the audio
}如果您想要实现实际的流,其中的部分音频文件被下载,然后播放,更多的会在以后需要时被获取,那么它就会变得更加复杂。请参阅“锈蚀食谱:https://rust-lang-nursery.github.io/rust-cookbook/web/clients/download.html#make-a-partial-download-with-http-range-headers”中有关部分下载的条目
我相信异步reqwest也可以更容易地完成,但我自己仍在试验。
https://stackoverflow.com/questions/63463503
复制相似问题