Connect to a music player daemon server, send commands, emit events.
You might also be interested in checking out node-groove, a generic music player backend as a node module.
Or maybe Groove Basin, a music player server which supports the MPD protocol and has many features and improvements over MPD.
import { MpdClient, parseKeyValueMessage } from "@cuadue/mpd";
const client = new MpdClient();
client.onSystem('player', async () => {
const msg = await client.getStatus();
console.log('System "player" event, status is now:', msg);
});
client.onSystem('mixer', async () => {
const msg = await client.getStatus();
console.log(`Volume changed to ${msg.volume}`);
});
client.on('state', (state) => {
if (state instanceof Error) {
console.log(`Something bad happened! ${state}`);
} else {
console.log(`State changed to ${state}`);
}
});
const main = async () => {
await client.connect({
port: 6600,
host: 'raspberrypi.local',
})
console.log("MPD Client is connected");
const playlistinfo = await client.sendCommands([
'clear',
['add', 'https://listen.xray.fm/stream'],
'play',
'playlistinfo'
]);
console.log('Playlist file:', parseKeyValueMessage(playlistinfo).file);
const msg = await client.getStatus();
console.log('Status', msg);
while (true) {
await new Promise<void>(resolve => setTimeout(resolve, 16));
}
};
main();
See also the MPD Protocol Documentation.
Make a new client with const client = new MpdClient()
.
Connects to the MPD server.
command
can be either:
- a string
commandName
for a command with no arguments, or - an array
[commandName, arg1, ...]
for a command with arguments
Send multiple commands in one batch
msg
: a string which contains an MPD response.
Returns an object.
Whenever the state of the client changes. arg
is
an Error
instance, the string connecting
or the string ready
.
A system has updated. systemName
is one of:
database
- the song database has been modified after update.update
- a database update has started or finished. If the database was modified during the update, the database event is also emitted.stored_playlist
- a stored playlist has been modified, renamed, created or deletedplaylist
- the current playlist has been modifiedplayer
- the player has been started, stopped or seekedmixer
- the volume has been changedoutput
- an audio output has been enabled or disabledoptions
- options like repeat, random, crossfade, replay gainsticker
- the sticker database has been modified.subscription
- a client has subscribed or unsubscribed to a channelmessage
- a message was received on a channel this client is subscribed to; this event is only emitted when the queue is empty
Callback is called when the client is ready to receive commands. This is called again after reconnecting, if there was an error previously.
Callback is called with no args when the subsystem having name
changes.