【问题标题】:Setting currentTime is not working while streaming audio via http通过 http 流式传输音频时设置 currentTime 不起作用
【发布时间】:2017-01-29 12:34:36
【问题描述】:

我正在使用 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 流的那个),seek 功能将正常工作,但如果我使用一个确实使用 http 流的音频文件,歌曲只会跳到开头。

我需要使用 http 流,因为有些文件非常大,无法将它们压缩/转码为更小的尺寸?

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

【问题讨论】:

    标签: javascript node.js express html5-audio audio-streaming


    【解决方案1】:

    浏览器需要知道允许搜索。

    尝试添加:

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

    或者,当您使用 Node Express 时,您可以添加一个有助于查找的插件,例如 this one

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 2015-05-04
      • 2020-12-31
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多