Music Player Daemon

From ArchWiki
Revision as of 03:41, 6 March 2009 by ST.x (talk | contribs) (missing file extension)
Jump to: navigation, search

Template:I18n links start Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n entry Template:I18n links end

What is mpd?

mpd (Music Player Daemon) is an audio player that has a server-client architecture. mpd runs in the background as a daemon, manages playlists and a music database, and uses very few resources. In order to interface with it, you need a separate client. More information can be found on their website

Daemon Install Procedure

Sync and install with pacman:

# pacman -S mpd

Configure mpd

For more information about mpd configuration visit

Timeline of mpd's behavior on a typical working setup

  • mpd is started on boot by /etc/rc.conf, by including it in the DAEMONS array. (Or, this can be done manually each session by running /etc/rc.d/mpd start with root privileges).
  • Since mpd is now started as root, it first reads the /etc/mpd.conf file.
  • mpd reads the user variable in the /etc/mpd.conf file, and changes from root to this user.
  • mpd then reads the contents of the /etc/mpd.conf file and configures itself accordingly.

Notice that mpd changes the running user from root to the one named in the /etc/mpd.conf file. This way, uses of "~" in the configuration file point correctly to the home user's directory, and not root's directory. It may be worthwhile to change all uses of "~" to "/home/username" to avoid any confusion over this aspect of mpd's behavior.

Quick Single User Installation

  • As root, check if /etc/mpd.conf exists and delete the file if it does. This is safe.

Mpd comes with an example configuration file, available at /etc/mpd.conf.example. This file holds an abundance of information on mpd configuration, and holds default mixer values you can simply uncomment.

  • As root, copy this example file to /etc/mpd.conf.
cp /etc/mpd.conf.example /etc/mpd.conf

Never put this file in the user's directory like some tutorials suggest. This would complicate things and most of the time doing this is useless (notice you're reading a quick installation howto). If you previously created a .mpdconf file in your home, remove it now. This is important to prevent conflicts. When placing it in /etc, like we do here, mpd will be able to run as a daemon at boot. Otherwise, a script would be needed to run mpd AFTER the user has logged (like kdm or ~/.fluxbox/startup) or it would require a manual move each time. For a single music collection, the method used here is simply better, even while the collection is shared to multiple users. Also, don't be afraid about root privileges: even while mpd runs as a daemon, it never completely runs as root since it automatically drops its root privileges after execution.

To make audio output actually work ensure that you have setup audio card and mixer correctly. See ALSA. Do not forget to unmute the required channels in alsamixer, raise the volume and save changes with alsactl store. See ~/.mpd/error if it still doesn't work.

Make sure your card can do hardware mixing (most of them can, including onboard audio). Or else this could cause problems with multiple sound playback. For example, this can prevent Mplayer from playing back sound while the mpd daemon is running, returning an audio error message stating the device is busy.

We can now move on to edit the file, once again, as root:

$EDITOR /etc/mpd.conf
  • In /etc/mpd.conf, look for these options and configure them as listed below:
music_directory       "~/Music"
playlist_directory    "~/Playlists"
db_file               "~/.mpd/db"
log_file              "~/.mpd/log"
error_file            "~/.mpd/error"
pid_file              "~/.mpd/"
state_file            "~/.mpd/mpdstate"
user                  "mpd" (replace this with your username)
bind_to_address       ""
port                  "6600"
#connection_timeout   "60"
#max_playlist_length  "16384"
#filesystem_charset   "ISO-8859-1"
#id3v1_encoding       "ISO-8859-1"

These are the most common lines. You shouldn't need anything else. You may want to change all paths to absolute paths, in order to avoid any ambiguities in the future in case you forget how mpd behaves (see above). See the file /etc/mpd.conf.example for more specific options.

  • Look at the line starting with user and replace mpd with your username. Save changes and exit the editor.
  • Now exit the root mode and log as the normal user (the same you specified in mpd.conf).
  • Create the new directories
$ mkdir ~/Music
$ mkdir ~/Playlists
$ mkdir ~/.mpd
  • Make symbolic links to your music collection:
$ ln -s /media/sda5/music_collection1 ~/Music
$ ln -s /media/sdb7/music_collection2 ~/Music
  • Create the pid file
$ touch ~/.mpd/
  • Create the mpd database (This can take a while depending on the size of your collection.):
$ mpd --create-db
  • Change back to root, and start mpd:
$ /etc/rc.d/mpd start

Although you can start mpd as root with just "mpd", doing the above gives more information (such as on htop).

  • Edit /etc/rc.conf and add mpd to the list of daemons to start on bootup.
  • To test it out, install a very small client called mpc
# pacman -S mpc        (as root)
$ mpc ls |  mpc  add   (as user)
$ mpc play

Template:Box Note


Note: in case of permission problems when using ESD with mpd run this as root:

# chsh -s /bin/true mpd


To get rid of timeouts (i.e. when you paused music for long time) in gpmc and other clients uncomment and increase connection_timeout option in mpd.conf.

If files and/or titles are shown in wrong encoding, uncomment and change filesystem_charset and id3v1_encoding options. Note that you cannot set encoding for ID3 v2 tags. To workaround this you may use external tag readers.

If you want to use another computer to control mpd over a network, the bind_to_address option in mpd.conf will need to be set to either your IP address, or any if your IP address changes frequently. Remember to add mpd to the /etc/hosts.allow file to enable external access.

Client Install Procedure

Install a client program for mpd. Popular options are:

Install with:

# pacman -Sy mpc
# pacman -Sy ncmpc
# pacman -Sy sonata
# pacman -Sy gmpc

Extra stuff scrobbling

To scrobble your songs to using MPD there are several alternatives. The easiest way, if you don't care about having to have a program window open all the time, is using Sonata which is a graphical frontend to MPD. It has built-in support for scrobbling in its preferences.

If you're not too keen on using Sonata (and having it open at all times) there is lastfmsubmitd in the Arch repositories. This is a simple daemon that keeps in contact with the MPD and submits track information to The post install notification says it all:

!!> You must edit /etc/lastfmsubmitd.conf for these daemons to work <!!
!!> You must also add lastfmsubmitd AND lastmp to your DAEMONS=()   <!!
!!> array for reporting to start on boot.                   <!!

Never play on start

If you don't want mpd to always play on your system start, but yet you want to preserve the other state information, add the following lines to your /etc/rc.d/mpd file:

   stat_busy "Starting Music Player Daemon"
   # always start in paused state
   awk '/^state_file[ \t]+"[^"]+"$/ {
       match($0, "\".+\"")
       sfile = substr($0, RSTART + 1, RLENGTH - 2)
   } /^user[ \t]+"[^"]+"$/ {
       match($0, "\".+\"")
       user = substr($0, RSTART + 1, RLENGTH - 2)
   } END {
       if (sfile == "")
       if (user != "")
               sub(/^~/, "/home/" user, sfile)
       system("sed -i \x27s|^\\(state:[ \\t]\\{1,\\}\\)play$|\\1pause|\x27 \x27" sfile "\x27")
   }' /etc/mpd.conf
   /usr/bin/mpd /etc/mpd.conf &> /dev/null

This will change the player status to "paused", if it was stopped while playing. Next, you want this file to be preserved, so mpd updates won't erase this edit. Add (or edit) this line to your /etc/pacman.conf:

NoUpgrade = etc/rc.d/mpd

MPD & Alsa

Sometimes, when you use other audio output, e.g: some web pages containing Flash applets, mpd cannot reproduce anything anymore (until you restart). The error looks something like: (if you search the file /var/log/mpd/mpd.error)

Error opening alsa device "hw:0,0": Device or resource busy

And here is the solution (dmix saving our life again). Apply these lines in your /etc/mpd.conf:

audio_output {
        type                    "alsa"
        name                    "Sound Card"
        options                 "dev=dmixer"
        device                  "plug:dmix"

And then restart with /etc/rc.d/mpd restart

Searching on Internet I found the reason for why that happens in Gentoo's wiki:

  • The sound card does not support hardware mixing (uses dmix plugin)
  • An application does not work with ALSA with it's default settings

Control mpd with lirc

There are already some clients designe for communications between lircd and mpd, however, as far as the practical use, they aren't very useful since their funcstions are limited.

It's recommended to use mpc with irexec. mpc is a command line player which only sends the command to mpd and exits immediately, which is perfect for irexec, the command runner included in lirc. What irexec does is that it runs a specified command once received a remote control button.

First of all, please setup your remotes as referred to the Lirc article.

Edit your favored lirc startup configuration file, default location is ~/.lircrc

Fill the file with the following pattern:

     prog = irexec
     button = <button_name>
     config = <command_to_run>
     repeat = <0 or 1>

An useful example:

## irexec
     prog = irexec
     button = play_pause
     config = mpc toggle
     repeat = 0

     prog = irexec
     button = stop
     config = mpc stop
     repeat = 0
     prog = irexec
     button = previous
     config = mpc prev
     repeat = 0
     prog = irexec
     button = next
     config = mpc next
     repeat = 0
     prog = irexec
     button = volup
     config = mpc volume +2
     repeat = 1
     prog = irexec
     button = voldown
     config = mpc volume -2
     repeat = 1
     prog = irexec
     button = pbc
     config = mpc random
     repeat = 0
     prog = irexec
     button = pdvd
     config = mpc update
     repeat = 0
     prog = irexec
     button = right
     config = mpc seek +00:00:05
     repeat = 0
     prog = irexec
     button = left
     config = mpc seek -00:00:05
     repeat = 0
     prog = irexec
     button = up
     config = mpc seek +1%
     repeat = 0
     prog = irexec
     button = down
     config = mpc seek -1%
     repeat = 0

There are more functions for mpc, run man mpc for more info.

External links