我正在使用 http 流式传输音频。我正在使用以下代码加载音频。
var audio = new Audio("http://example.com/get/stream/1/wav");
function seek(time){
audio.currentTime = time;
}
浏览器可以播放音频格式。
我正在运行一个 nodejs express 服务器来提供音频文件。下面是处理流的代码的简化版本。
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 流,因为有些文件非常大,无法将它们压缩/转码为更小的尺寸?
所以我的问题是:如何更改使用 http 流的音频文件的当前时间?
最佳答案
浏览器需要知道搜索是允许的。
尝试添加:
res.header("Accept-Ranges", "bytes");
或者,当您使用 Node Express 时,您可以添加一个有助于查找的插件,例如 this one .
关于javascript - 通过 http 流式传输音频时设置 currentTime 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41920946/