Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未调用音频进度事件_Javascript_Html_Node.js_Reactjs_Html5 Audio - Fatal编程技术网

Javascript 未调用音频进度事件

Javascript 未调用音频进度事件,javascript,html,node.js,reactjs,html5-audio,Javascript,Html,Node.js,Reactjs,Html5 Audio,因此,我在一个基于类的组件中得到了这个函数,该组件从api调用加载音频文件 loadAudio = () => { var x = new Audio("http://localhost:5000/getSong") x.addEventListener("progress", event => { console.log("progress"); }) this.setSta

因此,我在一个基于类的组件中得到了这个函数,该组件从api调用加载音频文件

loadAudio = () => {
        var x = new Audio("http://localhost:5000/getSong")
        x.addEventListener("progress", event => { console.log("progress"); })
        this.setState({ audioSrc: x })
    }
我从componentDidMount函数调用此函数

componentDidMount() {
    this.loadAudio()
}
音频加载,我可以使用
.play()
播放它,但是
进度
事件只显示两次,而当我使用浏览器点击URL并在vanilla JS中使用类似脚本时,文件会被缓冲多次。然后多次调用
进程

我需要知道加载的音频的值,这样我就可以像任何主流流媒体网站一样,将其与缓冲进度条连接起来

后端脚本供参考:

app.get('/getSong', cors(), function (req, res) {
    var path = require('path')
    var filePath = path.join(__dirname,"assets","b.mp3")
    console.log(filePath)

    var stat = fs.statSync(filePath);
    var readStream = fs.createReadStream(filePath);

    res.writeHead(200, {
        'Content-Type': 'audio/mpeg',
        'Content-Length': stat.size
    });
    readStream.pipe(res)
});

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))