You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用hound库从&[u8]字节数据创建WavReader实例?

解决方案

你可以用WavReader::from_reader方法,结合std::io::Cursor来包装&[u8]字节数据——因为Cursor<&[u8]>实现了std::io::Read trait,刚好符合from_reader的参数要求。

修改后的代码示例:

use hound::WavReader;
use std::io::Cursor;

const FILE: &[u8] = include_bytes!("input.wav");

fn main() {
    let cursor = Cursor::new(FILE);
    let mut reader = WavReader::from_reader(cursor).unwrap();
    
    // 后续可正常使用reader,比如读取音频样本
    for sample in reader.samples::<i16>() {
        println!("Sample: {:?}", sample.unwrap());
    }
}
  • 关键说明:
    • Cursor能将字节切片转换成支持定位操作的读取器,满足Read trait的约束
    • WavReader::from_reader是hound提供的通用读取入口,支持所有实现了Read的类型,不局限于文件路径

内容的提问来源于stack exchange,提问作者AZ Developers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 03:15:26