mpc.js is a javascript client library for the Music Player Daemon.
It features a Promise-based API for all mpd commands, type definitions for Typescript and works in both node.js and current browsers (connecting to mpd through a WebSocket bridge like websockify).
This is the node.js package containing the node-specific networking code.
Install with
npm install mpc-js
if you want to use it in node.js or
npm install mpc-js-web
if you want to use it in the browser and use your own module bundler (like webpack or browserify).
Then import it with
import { MPC } from 'mpc-js';
or
const MPC = require('mpc-js').MPC;
If you want to use it in the browser and don't use a module bundler, you can get a UMD build of
mpc.js as a minified javascript file from https://unpkg.com/mpc-js-web@latest/umd/mpc.min.js
.
You can also install this file with bower:
bower install MPC=https://unpkg.com/mpc-js-web@latest/umd/mpc.min.js
The npm package contains type definitions that will be picked up by the TypeScript compiler automatically.
Since you can't use TCP or Unix socket connections in the browser, you'll need to set up a WebSocket bridge, e.g.
websockify --web <path/to/your/webapp> 8000 localhost:6600
Now if you open your web application at http://localhost:8000/index.html
you can connect to mpd using
mpc.connectWebSocket('ws://localhost:8000/');
Typedoc-generated API documentation is available here.
The following events are emitted by the client:
ready
- The connection to mpd has been initializedsocket-error
- An error event from the underlying socket implementation, the error is passed to the event listenerssocket-end
- The socket was closed by mpdchanged
- There was a change in one or more of mpd's subsystems, the list of changed subsystems is passed to the event listeners. This list may contain:database
- the song database has been modified afterupdate
update
- a database update has started or finished; if the database was modified during the update, thedatabase
event is also emittedstored_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 likerepeat
,random
,crossfade
, replay gainsticker
- the sticker database has been modifiedsubscription
: 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
changed-<subsystem>
- There was a change in<subsystem>
Create a client and connect to mpd
const mpc = new MPC();
// connect via TCP (when running in node.js)
mpc.connectTCP('localhost', 6600);
// ... or a Unix socket (when running in node.js)
mpc.connectUnixSocket('/run/mpd/socket');
// ... or a WebSocket (when running in a browser)
mpc.connectWebSocket('ws://localhost:8000/');
The connect
methods will return a Promise that is resolved when the connection to mpd has been established or rejected when the connection attempt fails.
mpc.playback.play();
mpc.playback.next();
mpc.playback.stop();
Clear the playlist and add a directory
mpc.currentPlaylist.clear();
mpc.currentPlaylist.add('ambient/Loscil/2010 - Endless Falls');
Search the playlist for songs whose title contains 'dub' and delete them
mpc.currentPlaylist.playlistSearch('Title', 'dub').then(
items => items.forEach(item => mpc.currentPlaylist.deleteId(item.id)));
mpc.on('changed-player', () => {
mpc.status.status().then(status => {
if (status.state == 'play') {
mpc.status.currentSong().then(song => console.log(`Playing '${song.title}'`));
} else {
console.log('Stopped playback');
}
});
});
mpc.playback.play();
Playing 'Lake Orchard'
mpc.playback.stop();
Stopped playback
List the contents of a directory
mpc.database.listFiles('ambient/Loscil/2010 - Endless Falls').then(console.log);
[ File {
entryType: 'file',
path: '01. Endless Falls.mp3',
lastModified: 2014-07-03T18:28:07.000Z,
size: 19280819 },
File {
entryType: 'file',
path: '02. Estuarine.mp3',
lastModified: 2014-07-03T18:29:15.000Z,
size: 20292272 },
(...)
]
List metadata for the contents of a directory
mpc.database.listInfo('ambient/Loscil/2010 - Endless Falls').then(console.log);
[ Song {
entryType: 'song',
path: 'ambient/Loscil/2010 - Endless Falls/01. Endless Falls.mp3',
lastModified: 2014-07-03T18:28:07.000Z,
title: 'Endless Falls',
name: undefined,
artist: 'Loscil',
artistSort: undefined,
composer: undefined,
performer: undefined,
album: 'Endless Falls',
albumSort: undefined,
albumArtist: 'Loscil',
albumArtistSort: undefined,
track: '01/08',
disc: undefined,
date: '2010',
genre: 'Experimental, Ambient',
comment: undefined,
musicBrainzArtistId: undefined,
musicBrainzAlbumId: undefined,
musicBrainzAlbumArtistId: undefined,
musicBrainzTrackId: undefined,
musicBrainzReleaseTrackId: undefined,
duration: 475 },
(...)
]
List song titles from Loscil in 2006, grouped by album
mpc.database.list('Title', [['Artist', 'Loscil'], ['Date', '2006']], ['Album']).then(console.log);
Map {
[ 'Stases' ] => [ 'B15-A', 'Biced', 'Cotom', 'Faint Liquid', 'Micro Hydro', 'Nautical2',
'Resurgence', 'Sous-marin', 'Still Upon The Ocean Floor', 'Stratus', 'Subaquatic', 'Windless' ],
[ 'Plume' ] => [ 'Bellows', 'Charlie', 'Chinook', 'Halcyon',
'Mistral', 'Motoc', 'Rorschach', 'Steam', 'Zephyr' ],
[ 'Idol Tryouts Two: Ghostly International Vol. Two' ] => [ 'Umbra' ] }