首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >播放“锈蚀”中的url音频

播放“锈蚀”中的url音频
EN

Stack Overflow用户
提问于 2020-08-18 07:08:43
回答 1查看 2.3K关注 0票数 0

我已经使用rodio机箱从本地文件播放音频,通过浏览文档,但无法找到如何使用url播放音频。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-18 10:12:27

下面是一个使用阻塞reqwest的简单示例。这将在开始播放之前将整个音频文件下载到内存中。

代码语言:javascript
复制
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也可以更容易地完成,但我自己仍在试验。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63463503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档