Giter Club home page Giter Club logo

play-sound's Introduction

play-sound

Downloads

Play sounds by shelling out to one of the available audio players.

Installation

npm install play-sound

Examples

var player = require('play-sound')(opts = {})

// $ mplayer foo.mp3 
player.play('foo.mp3', function(err){
  if (err) throw err
})

// { timeout: 300 } will be passed to child process
player.play('foo.mp3', { timeout: 300 }, function(err){
  if (err) throw err
})

// configure arguments for executable if any
player.play('foo.mp3', { afplay: ['-v', 1 ] /* lower volume for afplay on OSX */ }, function(err){
  if (err) throw err
})

// access the node child_process in case you need to kill it on demand
var audio = player.play('foo.mp3', function(err){
  if (err && !err.killed) throw err
})
audio.kill()

Options

Prior art

  • play.js - play sound files from node.js to your speakers

License

MIT

play-sound's People

Contributors

dependabot[bot] avatar ineluki avatar jfix avatar jirick1 avatar jjgonecrypto avatar michaelhogg avatar michmich avatar mykeels avatar shime avatar tonydewan avatar unional avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

play-sound's Issues

Universal volume control?

Is there a way to adjust the volume for all types of players, not just a specifically named one? The example in the README says { afplay: ['-v', 1 ] /* lower volume for afplay on OSX */ }, but I don't want to just change afplay. I'd like to change whatever player is being used.

does not working in orangepi zero 2 under root

i have this code:

const PlayFile =(FileName)=>{
    var audfile = tpath.join(__dirname,'../database/storage/media/'  ,FileName);
    console.log(' play ', audfile);
    if(doplay){
        doplay = false;
        localPlayer = player.play(audfile, function(err){
            console.log('end play')
            doplay = true;
            if (err) console.log("Error play" ,err);
        })
    }
}

when i run it normaly every thing works, but when i run it under root it gives me an error 1

i tried to make the file only for root user and for every user but steel not playing

[BUG] Sound is looped

Hello.

Sound is being looped. I'm running the sound on a setInterval at 80bpm.
It plays good at first, then it starts to loop multiple times.

I tried setting the interval function as async and then calling await on the player.play().
Didn't solve it.

Hope you can help, thanks.

OS: Windows 10 Pro 64bit
CLI Audio Player: Mplayer

Play mp3 on Windows computer

Hello, I would like to play mp3 files on a Windows computer.
I have installed the MPlayer and it also works when I call a track explicitly
mplayer "C:\Users\xyz\Downloads\MP3\Rock\test.mp3"

When I start the test, I get the message
"Error playing back file C:\Users\xyz\Downloads\MP3\Rock/test.mp3: 1"
\ and / are mixed and nothing is playing.

How do I have to store the directory and the files in the node?

here are some more questions:
How can I play the whole directory?
can I deposit my playlists?
can I use the commands start / stop / next / pause etc.?

I would be very happy about a feedback
Thanks already
Roland

Should accept URL

In the case of mplayer at least, a URL is valid input. It'd be great of play-sound would accept a URL too!

Wait till the current file has been played till the end and then play the next audio file

Does play-sound provide an option to know whether the player is running or not? As my function is runing at 3 times per second, if there are two audio files to be played then the second audio file breaks the player playing the first audio file and then plays the second file.

Is there a way to let the player play till the end of the current audio file and then play the next file in the queue?

Stop function (and aplay)

Noticed you took over play.js (after noticing it was a bit nonactive), nice job! 👍

An open issue there was a request for stop functionality, thought of mentioning it here too as it still seems pretty relevant to this package.

Could probably be done with spawn instead of exec, hold reference and kill child, may possible also (partially) resolve #12.

(Also, was thinking, perhaps aplay could be added to the default players? It is pretty common on Linux systems)

No audio on windows

I'm not getting any audio playback on windows. I installed mplayer and set it as the player manually; it looks like it's properly loading the sound but no audio is playing. Any one else get this issue?

Is this project dead?

In case yes I suggest trying to find someone to take over. You got 5 pull requests unanswered.

work via browserify

it would be cool if (when in the browser) this would create a new audio tag to play the sound

Sound in tests

When I run the tests should I also be able to hear a sound? I know that sounds like a stupid question because there's no audio receiver to validate that a sound was played but I feel that it would be a useful cue for a person testing to see if an appropriate player were on their workstation.

From what I can see you've mocked all the play functionality so there wouldn't be a sound when a test is run.

Given that this module is all about sound I think that you have some leway in your tests and it would be neat if the test suite started off with an audible "starting" or "go" and "done" or "finished" at the end.

Audio doesn't play when play function is inside other callbacks (using bleno library)

Good afternoon and thank you for your work.

I'm trying to play an audio file when I receive a value from a bluetooth characteristic on a Raspberry Pi. I'm using bleno for bluetooth.
I can play files following the example, but I cannot play them when inside the bluetooth loop and I don't know how to debug that. I'm not sure if it's because of being inside another process or because of something related with the event loop, or maybe because of the path of the file, but it does not throw an error. Also I'm using a setTimeout function with a number coming from bluetooth, but I've done that before without any issues.

This is the file in which I try to play an mp3 file:

const player = require('play-sound')();
module.exports.squeduleSound = function (seconds) {
    setTimeout(function() {
	playSound();
    }, seconds*1000);
};

function playSound() {
        console.log('Playing sound');
        player.play('./sound.mp3', (err) => {
                if (err) console.log('Could not play sound: ${err}');
	});
}

And this is the bluetooth callback, the onWriteRequest method is part of the bleno library:

var soundSqueduler = require('../audioPlayer/audioScheduler');
WriteTimeCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
	this._value = data;
    	var seconds =  this._value.toString('utf8');
    	console.log('CustomCharacteristic - onWriteRequest: value = ' + seconds);
    	soundSqueduler.squeduleSound(seconds);
	callback(this.RESULT_SUCCESS);
};

I'm using node version: 8.11.1. (I can't use a newer one because of bleno. But I've done this in the past with this version.)
bleno version: 0.5.0
play-sound: 1.1.3

Thank you

Unable to play

I've run into an error when I try to run this code.

let player = require('play-sound')(opts = {});

player.play('Haywyre - Contagious.mp3', function (err) {
    if (err) throw err;
});
INFO: Could not find files for the given pattern(s).
INFO: Could not find files for the given pattern(s).
INFO: Could not find files for the given pattern(s).
INFO: Could not find files for the given pattern(s).
INFO: Could not find files for the given pattern(s).
INFO: Could not find files for the given pattern(s).
INFO: Could not find files for the given pattern(s).
INFO: Could not find files for the given pattern(s).

Is anyone able to provide some insight into this?

Error: 1

const player = require('play-sound')(opts = {});


player.play('path/to/soundfile.mp3', (err) => {
  if (err) {
    console.error('Error occurred: ', err);
  }
});

When I try to run the code, no sound is played and instead the console throws an error saying

Error occured: 1

Error: stdOut maxBuffer exceeded

Hi,

I'm getting the error in the title when play-sound uses something like mplayer to play. The error is defined a bit here: robrich/gulp-exec#27 (not sure if play-sound uses that module or not).

My guess is that the "progress bar" (not really, just a count of the elapsed time) that mplayer generates is filling the output buffer for the process. Using something like afplay (which doesn't output anything at all) works perfectly.

Not sure how to fix it, but if possible, piping all the output of the commands to something like /dev/null (for UNIX systems, obviously :D) should fix the issue (or adding a parameter to every supported player to work in silent mode, but I'm not sure that's possible for all of them :P)

err.killed returns undefined

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).

Unable to use arguments with portable executables

Say I have a portable executable downloaded, and adjust my code as such:

var player = require('play-sound')({player: "/path/to/downloads/mplayer/mplayer.exe"})
player.play('foo.mp3', { "mplayer": ['-loop', 0] }, function(err){})

I'm unable to get the arguments to work.
However, if I adjust my code to use mplayer that has been installed on the command line, the arguments work and the song loops

var player = require('play-sound')({player: "mplayer"})
player.play('foo.mp3', { "mplayer": ['-loop', 0] }, function(err){})

So, how do I get the args to work on a portable mplayer? It plays but doesn't loop.

Last second of output is cut off using 'play' as player

On Raspberry Pi 3B, using 'play' as player (which play-sound defaulted to in my case), the last second of audio output is cut off.

A .wav file saying 'four thousand' will sound like 'four thousa'

Setting {'player: 'play'} in the options has the same effect.
Playing the sound through play manually in the CLI (without args, just 'play output.wav') gives full audio output.

Switching to aplay (alsa-utils) resolves the issue, which was my prefered player anyway, so to me it's not really an issue...
Others might disagree :x

I can easily switch back and test more, report output or pass back more information about my setup.

RaspberryPi support

I get errors when installing in RaspberryPi, something about the execSync.

not working in windows 10 x64

const player = require('play-sound')();

player.play('alert.mp3', (err) => { if (err) console.log(Could not play sound: ${err}); });
Error: Couldn't find a suitable audio player
Error: INFO: Could not find files for the given pattern(s).

Thank you

ERROR: ReferenceError: isURL is not defined

Hi there,
I got this error when I use this npm with node --use_strict in my app

/Users/kstefan/DSTIL/Code/mqtt-play/node_modules/play-sound/index.js:23
isURL = this.player == 'mplayer' && this.urlRegex.test(what)
   ^

ReferenceError: isURL is not defined

Any idea?

Cheers,

Stefanus

How to solve this err?

`C:\Users\Cooper\Desktop\pp\app.js:5
if (err) throw err
^

Error: Couldn't find a suitable audio player
at Play.play (C:\Users\Cooper\node_modules\play-sound\index.js:34:19)
at Object. (C:\Users\Cooper\Desktop\pp\app.js:4:8)
at Module._compile (module.js:662:30)
at Object.Module._extensions..js (module.js:673:10)
at Module.load (module.js:575:32)
at tryModuleLoad (module.js:515:12)
at Function.Module._load (module.js:507:3)
at Function.Module.runMain (module.js:703:10)
at startup (bootstrap_node.js:193:16)
at bootstrap_node.js:660:3`

Cannot play Audio from url

I've been trying all day to find a simple module that plays sound without a bunch of errors in nodejs, and play-sound has been one of the best so far. It works fine playing local audio, but when I try to play audio from a URL, it throws an error. I would prefer not to download the file to the user's computer then play it.

I am using this code and it throws an error:
player.play('https://audiocdn.bithop.stream/piano/0g.mp3', function (err) { if (err) throw err; console.log("Audio finished"); });

Windows looping sound?

When I play a sound it keeps looping and it never ends...
Dose anyone have the same problem?

var player = require('play-sound')(opts = {player: "mplayer"})
player.play("test.wav", function(err){
  if (err){
    console.error(err);
  }
})

Opts is not defined

When I try to use the module it throws an error....

ReferenceError: opts is not defined

Not sure whats going on here.... when I try to remove (opts = {}) then it says there is no method called .play() can't seem to get around this error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.