shime / play-sound

Play sounds by shelling out to one of the available audio players.
MIT License
207 stars 31 forks source link

err.killed returns undefined #23

Open Komoszek opened 6 years ago

Komoszek commented 6 years ago

Since version 1.1.2 version you can't check if process was killed because err.killed always returns undefined (err is only a number, not an object).

ZTiKnl commented 5 years ago

confirmed, same problem here

example code:

const sfx = require('play-sound')(opts = {player: "aplay"})
var voiceoutput;

voiceoutput = sfx.play('./output.wav', function(err){
  if (err) {
    console.log('Couldn\'t play: already playing');
    if (err.killed) {
      console.log('Stopped talking');
    }
  }
  voiceoutput = null;
  return
})

function silence() {
  if (voiceoutput != null) {
    voiceoutput.kill();
  } else {
    console.log('audio output already stopped');
  }
}
ZTiKnl commented 5 years ago

This item can be marked 'solved', all that is required is an update to the example/readme file.

replace:

var audio = player.play('foo.mp3', function(err){
  if (err && !err.killed) throw err
})
audio.kill()

with:

var audio = player.play('foo.mp3', function(err){
  if (err && !audio.killed) throw err
})

audio.kill()
  // audio.killed == true 

in my example above, the code would change to:

if (voiceoutput.killed) {
  console.log('Stopped talking');
}

@Komoszek tagging you in case you still need this on an old project ;)

ZTiKnl commented 5 years ago

Although on second thought, perhaps err should be more than 1, so maybe not 'solved' just yet...

hasanbasri1993 commented 4 years ago

not working .. with kill() hhm

so i use https://www.npmjs.com/package/tree-kill audio = player.play('../media/jaros.mp3', false, function (err) { if (err && !audio.killed) throw err; }); pids.push(audio.pid);

for (let i = 0; i < pids.length; i++) { kill(pids[i], 'SIGKILL', function (err) { }); }