Catherine S.
Catherine S.

Reputation: 229

How to play wav file in Node.Js?

I need something simple: play an audio file from my node.js file. I've tried all sorts of things and cannot find anything that works. I'm currently trying "play-sound", from this link: https://www.npmjs.com/package/play-sound

I made sure to install play-sound, and then I have just the following as my code:

var player = require('play-sound')(opts = {})
player.play('chime.wav', function(err){
if (err) throw err
});

I get nothing. I can play the sound just fine from the command line with:

aplay chime.wav

Any ideas would be appreciated.

Update: I figured it out. "node-aplay" worked for me: https://www.npmjs.com/package/node-aplay

I didn't need the USB audio configuration part. I just installed node-aplay and alsa according to the instructions. My code was just the first two lines in the example:

var Sound = require('node-aplay');

// fire and forget: 
new Sound('/path/to/the/file/filename.wav').play();

Upvotes: 3

Views: 13104

Answers (3)

aring
aring

Reputation: 3602

You could see what was happening by checking stdout or stderr:

var player = require('play-sound')(opts = {})
player.play('chime.wav', function(err, stdout, stderr) {
   if (err) throw err
   console.log(stdout)                                                                                               
   console.log(stderr)
});

Upvotes: 0

lehoang
lehoang

Reputation: 4149

Try sound-play instead, it supports both .wav and .mp3:

const sound = require('sound-play')
sound.play('/path/music.wav')

Upvotes: 1

Catherine S.
Catherine S.

Reputation: 229

Update: I figured it out. "node-aplay" worked for me: https://www.npmjs.com/package/node-aplay

I didn't need the USB audio configuration part. I just installed node-aplay and alsa according to the instructions. My code was just the first two lines in the example:

var Sound = require('node-aplay');

// fire and forget: 
new Sound('/path/to/the/file/filename.wav').play();

Upvotes: 1

Related Questions