Troubadour
Troubadour is wrapper over command-line audio players that makes it easy to play audio in Node.js. At this point, the following audio players are supported:
- mpg123
- MPlayer
- SoX
- VLC
Other audio players can easily be added by adding entries into the supportedAudioPlayers.json
file.
Installation
npm install troubadour
Since Troubadour uses command-line audio players to facilitate audio playback, you will also need to install one of the supported players.
Usage
Getting Started
To get started, require the module in your program and create a new Troubadour instance by passing the name of the supported audio player into the constructor.
const Troubadour = ;const troubadour = 'sox';
Troubadour is an event emitter that indicates when certain actions are being performed by the audio player. The following code snippet shows how to add an event listener to your new troubadour
player.
troubadour;
You do not need to have listeners set up in order to use Troubadour but they provide a programmatic way of defining actions that you would like to take when the audio playback starts, pauses, stops, resumes, ends, and errors.
Play
To play an audio source, use the play
function and pass in the file path of the audio source that is to be played:
troubadour; troubadour; troubadour;
Pause
Pause the playback (with the ability to resume later) by using the pause
function:
troubadour; troubadour;
Stop
Stop the playback (without the ability to resume later) by using the stop
function:
troubadour; troubadour;
Resume
Resume the playback from a paused state by using the resume
function:
troubadour; troubadour;
Errors
Troubadour handles errors in two different ways: abruptly throwing an error when the error is not recoverable or gracefully emitting the error when it is recoverable.
Errors like not passing the name of a supported command-line audio player to the Troubadour constructor are not recoverable, so the module will throw an error. Errors like calling the play
function without specifying a file path are recoverable, so Troubadour will just emit an error event to let the user know.
The error listener can be set up by including the following:
troubadour;
The error
parameter in the callback listener function is an error object. Just like any error object, accessing the error message can be done through error.message
.