【问题标题】:How to read data as buffer from an audio file as soon as it is added to the file in nodejs?一旦将音频文件添加到nodejs中的文件中,如何从音频文件中读取数据作为缓冲区?
【发布时间】:2019-11-22 08:54:39
【问题描述】:

我有一个正在现场录制的 .wav 音频文件。我只想在数据添加到其中后立即读取它的数据并发送到其他服务器(或网络浏览器以实时收听)

如何在 nodejs 中做到这一点。

【问题讨论】:

    标签: node.js file streaming http-live-streaming


    【解决方案1】:

    TL;DR:创建 .wav 文件的读取流,并将其与写入流通过管道传输。

    这不是一个实际的工作示例。这只是为了演示如何实现它。

    这里,res 是一个写流!

    const fs = require('fs');
    const file = fs.createReadStream('audio.wav');
    
    app.get('/', (req, res) => {
      file.on('data', (chunk) => {
        // Send chunk to client
        res.send(chunk); // May be?
      });
    });
    

    或者,您可以使用管道!

    app.get('/', (req, res) => {
      file.pipe(res);
    });
    

    【讨论】:

      猜你喜欢
      • 2014-11-14
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多