Searene
Searene

Reputation: 27544

How to play audio stored in Buffer in nodejs?

I have unzipped the contents of a wav file into buffer in nodejs, and I would like to play it without writing the buffer into disk, is it possible?

SAMPLE CODE

const fileContents: Buffer = readAudioFileFromZip();
playAudioFromBuffer(fileContents); // is it possible to write this function?

Upvotes: 5

Views: 6517

Answers (1)

TGrif
TGrif

Reputation: 5921

Yes, it is possible.
One way to do this is to use the excellent module node-speaker from TooTallNate.

Here is a simple example:

const stream = require('stream');
const Speaker = require('speaker');

let speaker = new Speaker();

function playAudioFromBuffer(fileContents) {
  let bufferStream = new stream.PassThrough();
  bufferStream.end(fileContents);
  bufferStream.pipe(speaker);
}

playAudioFromBuffer(fileContents)

Upvotes: 2

Related Questions