通过 http 流式传输音频时设置 currentTime 不起作用

Setting currentTime is not working while streaming audio via http

我正在使用 http 流式传输音频。我正在使用以下代码加载音频。

var audio = new Audio("http://example.com/get/stream/1/wav");
function seek(time){
    audio.currentTime = time;
}

浏览器可以播放音频格式。

我是 运行 一个用于提供音频文件的 nodejs 快速服务器。下面是处理流的代码的简化版本。

app.get("/get/stream/:id/:format", function(req, res){
    //Here was some code to verify the request, this code doesn't affect this question so I left it out

    var stat = fs.statSync(__dirname+"/path/to/file/here.wav");

    res.header("Content-Type", "audio/wav");
    res.header("Content-Length", stat.size);

    var readStream = fs.createReadStream(__dirname+"/path/to/file/here.wav");
    readStream.pipe(res);
});

如果我加载一个普通的音频文件(一个不使用 http 流的文件),搜索功能将正常工作,但如果我使用一个使用 http 流的音频文件,歌曲将直接跳到开头。

我需要使用 HTTP 流式传输,因为有些文件非常大,compressing/transcoding 不能将它们缩小到更小的尺寸吗?

所以我的问题是:如何更改使用 http 流的音频文件的当前时间?

浏览器需要知道允许查找。

尝试添加:

res.header("Accept-Ranges", "bytes"); 

或者,当您使用 Node Express 时,您可以添加一个 plug-in 来帮助查找,例如 this one.