Difference between revisions of "Music Player Daemon"

From ArchWiki
Jump to: navigation, search
m (Scripted Configuration)
(simplification and beautification of wikilinks, fixing whitespace, capitalization and section fragments (https://github.com/lahwaacz/wiki-scripts/blob/master/link-checker.py (interactive)))
 
(93 intermediate revisions by 41 users not shown)
Line 1: Line 1:
[[Category:Player]]
+
[[Category:Multimedia players]]
 
[[de:Music Player Daemon]]
 
[[de:Music Player Daemon]]
 
[[es:Music Player Daemon]]
 
[[es:Music Player Daemon]]
 
[[fr:MPD]]
 
[[fr:MPD]]
 
[[it:Music Player Daemon]]
 
[[it:Music Player Daemon]]
 +
[[ja:Music Player Daemon]]
 
[[nl:Music Player Daemon]]
 
[[nl:Music Player Daemon]]
 
[[pl:Music Player Daemon]]
 
[[pl:Music Player Daemon]]
 
[[ru:Music Player Daemon]]
 
[[ru:Music Player Daemon]]
 
[[sr:Music Player Daemon]]
 
[[sr:Music Player Daemon]]
[[tr:Music_Player_Daemon]]
+
[[tr:Music Player Daemon]]
 
[[zh-CN:Music Player Daemon]]
 
[[zh-CN:Music Player Daemon]]
{{Article summary start}}
+
{{Style|Needs improvements in structure and clarify; compare to the [http://www.musicpd.org/doc/user/index.html User's manual].|section=Style comment}}
{{Article summary text|Installation, configuration and basic troubleshooting of MPD.}}
+
{{Article summary heading|Related Articles}}
+
{{Article summary wiki|MPD/Tips and Tricks}}
+
{{Article summary wiki|MPD/Troubleshooting}}
+
{{Article summary heading|Other sources}}
+
{{Article summary text|[[Wikipedia:Music Player Daemon|Wikipedia article]]}}
+
{{Article summary end}}
+
  
'''[http://mpd.wikia.com MPD]''' ('''m'''usic '''p'''layer '''d'''aemon) is an audio player that has a server-client architecture.  It plays audio files, organizes playlists and maintains a music database all while using  very few resources. In order to interface with it, a separate [[#Clients|client]] is needed.
+
{{Related articles start}}
 +
{{Related|MPD/Tips and Tricks}}
 +
{{Related|MPD/Troubleshooting}}
 +
{{Related articles end}}
 +
 
 +
'''[http://www.musicpd.org/ MPD]''' ('''m'''usic '''p'''layer '''d'''aemon) is an audio player that has a server-client architecture.  It plays audio files, organizes playlists and maintains a music database all while using  very few resources. In order to interface with it, a separate [[#Clients|client]] is needed.
  
 
== Installation ==
 
== Installation ==
=== Stable version ===
 
The latest stable version of {{Pkg|mpd}} is available in the [[official repositories]].
 
  
=== Git version ===
+
[[Install]] the {{Pkg|mpd}} package, or {{AUR|mpd-git}} for the development version.
Should users wish to run an experimental version, the [[AUR]] offers several from which to choose. For example, {{AUR|mpd-git}}.
+
 
 +
{{Note|An alternative plug-in based implementation called [http://www.mopidy.com Mopidy] exists. It is available as {{Pkg|mopidy}} and {{AUR|mopidy-git}}. Be warned that is not a complete MPD [http://docs.mopidy.com/en/latest/ext/mpd/#limitations drop-in replacement].}}
  
 
== Setup ==
 
== Setup ==
Line 32: Line 30:
 
MPD is able to run locally (per user settings), globally (settings apply to all users), and in multiple instances.  The way of setting up mpd depends on the way it is intended to be used: a local configuration may prove more useful on a desktop system, for example.
 
MPD is able to run locally (per user settings), globally (settings apply to all users), and in multiple instances.  The way of setting up mpd depends on the way it is intended to be used: a local configuration may prove more useful on a desktop system, for example.
  
For a proper MPD operation these are the necessary files and directories:
+
In order for MPD to be able to play back audio, [[ALSA]] or [[OSS]] (optionally with [[PulseAudio]]) needs to be setup and working.
  
* mpd.db - The music database
+
MPD is configured in {{ic|mpd.conf}}. The location of this file depends on how you want to run MPD (see the sections below). These are commonly used configuration options:
* mpd.pid -  The file where mpd stores its process ID
+
* {{ic|pid_file}} -  The file where mpd stores its process ID
* mpd.log - mpd logs here
+
* {{ic|db_file}} - The music database
* mpdstate - mpd's current state is noted here
+
* {{ic|state_file}} - MPD's current state is noted here
* playlists - the folder where playlists are saved into
+
* {{ic|playlist_directory}} - The folder where playlists are saved into
 +
* {{ic|music_directory}} - The folder that MPD scans for music
 +
* {{ic|sticker_file}} - The sticker database
  
In order for MPD to be able to play back audio, [[ALSA]], [[PulseAudio]] or [[OSS]] needs to be setup and working.
+
=== Global configuration ===
  
=== Local Configuration (per user) ===
+
{{Warning|Users of PulseAudio with a global mpd have to implement a [[Music Player Daemon/Tips and tricks#Local (with separate mpd user)|workaround]] in order to run mpd as its own user!}}
  
MPD can be configured per user (rather than the typical method of configuring MPD globally). Running MPD as a normal user has the benefits of:
+
The default {{ic|/etc/mpd.conf}} keeps the setup in {{ic|/var/lib/mpd}} which is assigned to user as well as primary group ''mpd''.
  
* A single directory {{ic|~/.mpd}} (or any other directory under {{ic|/home/$USER/}}) that will contain all the MPD configuration files.
+
==== Music directory ====
* Easier to avoid unforeseen read/write permission errors.
+
  
To setup: create a directory for the required files and the playlists; copy the example configuration locally; create all of the requisite files:
+
The music directory has to be set by parameter {{ic|music_directory}} in file {{ic|/etc/mpd.conf}}:
 +
music_directory "/path/to/music"
  
mkdir -p ~/.mpd/playlists
+
MPD needs to have {{ic|+x}} permissions on ''all'' parent directories to the music collection and also read access to all directories containing music files. This conflicts with the default configuration of the user directory where many users store their music.
cp /usr/share/doc/mpd/mpdconf.example ~/.mpd/mpd.conf
+
touch ~/.mpd/{database,log,state}
+
  
Edit {{ic|~/.mpd/mpd.conf}} and specify the requisite files:
+
While there are several solutions to this problem one of these should be most practical:
 +
* [[#Local configuration .28per user.29|run MPD as user]]
 +
* add the mpd user to your login group and grant group permission to your user directory:
 +
  # gpasswd -a mpd <your login group>
 +
  $ chmod 710 /home/<your home dir>
 +
* put your music collection to a different path (a) by moving it entirely, (b) with a bind mount or (c) with a [[Btrfs#Subvolumes|Btrfs subvolume]] (you should make this change persistent with an entry to {{ic|/etc/fstab}} ). Permissions of alternate directories can be adjusted with [[Access Control Lists]].
  
{{hc|~/.mpd/mpd.conf|
+
The MPD config must contain only one music directory. If the music collection is contained under multiple directories, create symbolic links under the main music directory in {{ic|/var/lib/mpd}}. Remember to set permissions accordingly on the directories being linked.
music_directory    "~/music"          # Can keep commented if XDG music dir.
+
playlist_directory "~/.mpd/playlists"
+
db_file            "~/.mpd/database"
+
log_file          "~/.mpd/log"
+
pid_file          "~/.mpd/pid"
+
state_file        "~/.mpd/state"
+
}}
+
  
MPD can now be started by typing {{ic|mpd}} on the command line (mpd first searches for {{ic|~/.mpd.conf}}, then {{ic|~/.mpd/mpd.conf}}, and then for {{ic|/etc/mpd.conf}} [there is no support for XDG-config directory {{ic|~/.config/mpd/mpd.conf}}]).  To specify the location of the configuration file :
+
==== Start MPD ====
  
mpd ~/.config/mpd/mpd.conf
+
MPD can be controlled with {{ic|mpd.service}} [[systemd#Using units|using systemd]]. The first startup can take some time as MPD will scan your music directory.
  
To start MPD on login add to {{ic|~/.profile}} (or another [[Autostarting#Shells|Autostart file]]):
+
Test everything by starting a client application ({{Pkg|ncmpc}} is a light and easy to use client), and play some music!
  
# MPD daemon start (if no other user instance exists)
+
===== Socket activation =====
[ ! -s ~/.mpd/mpd.pid ] && mpd
+
  
To start with the X.org server add to either [[xprofile]] or [[xinitrc]].  Some DEs ignore these files (GNOME does) and a desktop file must be placed in {{ic|~/.config/autostart/mpd.desktop}}:
+
If the {{ic|mpd.socket}} unit (provided by {{Pkg|mpd}}) is enabled while {{ic|mpd.service}} is disabled, systemd will not start mpd immediately, but it will listen on the appropriate sockets. When an mpd client attempts to connect on one of those sockets, systemd will start {{ic|mpd.service}} and transparently hand over control of those ports to the mpd process.
  
<pre>[Desktop Entry]
+
If you prefer to listen on different UNIX sockets or network ports (even multiple sockets of each type), or if you prefer not to listen on network ports at all, [[systemd#Editing provided units|edit]] the {{ic|mpd.socket}} unit appropriately '''and''' modify {{ic|/etc/mpd.conf}} to match the configuration (see {{ic|man 5 mpd.conf}} for details).
Encoding=UTF-8
+
Type=Application
+
Name=Music Player Daemon
+
Comment=Server for playing audio files
+
Exec=mpd
+
StartupNotify=false
+
Terminal=false
+
Hidden=false
+
X-GNOME-Autostart-enabled=false</pre>
+
  
==== Autostart with systemd ====
+
==== Configure audio ====
  
{{Note|1=As of 2013-03-20 this functionality is still in development ([https://bbs.archlinux.org/viewtopic.php?pid=1208585 forum thread]).  However, there is a workaround though it is peculiar as it requires a running X.org server to work.}}
+
Users of [[ALSA]] will want to have the following device definition; replace {{ic|My Sound Card}} with the name of a sound card or pcm ({{ic|aplay --list-pcms}}).
 +
{{hc|/etc/mpd.conf|2=
 +
audio_output {
 +
        type            "alsa"
 +
        name            "My Sound Card"
 +
        mixer_type      "software"      # optional
 +
}
 +
}}
  
'''Expected setup'''
+
The {{ic|mixer_type "software"}} option tells 'mpd' to use its own independent software volume control.
  
* Start user-session service (e.g. {{ic|sudo systemctl enable user-session@USERNAME}}).
+
Users of [[PulseAudio]] will need to make the following modification:
* Add local service file {{ic|~/.config/systemd/user/mpd.service}} (see below).
+
{{hc|/etc/mpd.conf|2=
* Start local systemd {{ic|systemd --user &}}; enable service file {{ic|systemctl --user enable mpd}}; start it {{ic|systemctl --user start  mpd}}.
+
audio_output {
* Add {{ic|systemd --user &}} to {{ic|~/.profile}} or another autostart file.
+
        type            "pulse"
* Reboot and MPD start.
+
        name            "pulse audio"
 +
}
 +
}}
  
<pre>[Unit]
+
PulseAudio supports multiple advanced operations, e.g. transferring the audio to a different machine. For advanced configuration with MPD see [http://mpd.wikia.com/wiki/PulseAudio Music Player Daemon Community Wiki].
Description=Music Player Daemon
+
  
[Service]
+
==== Changing user ====
ExecStart=/usr/bin/mpd %h/.config/mpd/mpd.conf --no-daemon
+
ExecStop= /usr/bin/mpd %h/.config/mpd/mpd.conf --kill
+
  
[Install]
+
Changing the group that MPD runs as may result in errors like {{ic|output: Failed to open "My ALSA Device"}}, {{ic|[alsa]: Failed to open ALSA device "default": No such file or directory}} or {{ic|player_thread: problems opening audio device while playing "Song Name.mp3"}}.
WantedBy=default.target</pre>
+
  
'''Current setup''' (this hasn't worked for everybody)
+
This is because the MPD users need to be part of the ''audio'' group to access sound devices under {{Ic|/dev/snd/}}. To fix it add user make the MPD user part of the ''audio'' group:
 +
# gpasswd -a '''mpd''' audio
  
As recommended above, it is best to use MPD as a service per user. In this case, we will use {{ic|~/.mpd/mpd.conf}} and not start mpd.service as a daemon for the whole system and all users.
+
==== Timeline of MPD startup ====
We will NOT use the {{ic|/usr/lib/systemd/system/mpd.service}} which is intended to start the mpd.service as root and for all users.
+
  
{{Note|It is recommended to follow the instructions in the [[Systemd/User]] page fully, and to have your whole session managed by {{ic|systemd --user}}.}}
+
To depict when MPD drops its superuser privileges and assumes those of the user set in the configuration, the timeline of a normal MPD startup is listed here:
  
If you already enabled it, just disable it first:
+
# Since MPD is started as root by systemd, it first reads the {{ic|/etc/mpd.conf}} file.
{{bc| # systemctl disable mpd.service}}
+
# MPD reads the user variable in the {{ic|/etc/mpd.conf}} file, and changes from root to this user.
 +
# MPD then reads the contents of the {{ic|/etc/mpd.conf}} file and configures itself accordingly.
  
If you used to start mpd inside your {{ic|~/.xinitrc}}, comment or delete the line
+
Notice that MPD changes the running user from root to the one named in the {{ic|/etc/mpd.conf}} file.
{{bc|mpd ~/.mpdconf}}
+
This way, uses of {{ic|~}} 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 {{ic|~}} to {{ic|/home/username}} to avoid any confusion over this aspect of MPD's behavior.
  
Then, edit a new file ~/.config/systemd/user/mpd.service
+
=== Local configuration (per user) ===
{{hc|~/.config/systemd/user/mpd.service|
+
[Unit]
+
Description &#61; Music Player Daemon
+
  
[Service]
+
MPD can be configured per user (rather than the typical method of configuring MPD globally). Running MPD as a normal user has the benefits of:
ExecStart &#61; /usr/bin/mpd --no-daemon
+
Restart &#61; always
+
  
[Install]
+
* A single directory {{ic|~/.config/mpd/}} (or any other directory under {{ic|$HOME}}) that will contain all the MPD configuration files.
WantedBy &#61; default.target
+
* Easier to avoid unforeseen read/write permission errors.
}}
+
  
Then, add this line to [[xinitrc|.xinitrc]] before any exec <session-command>
+
Good practice is to create a single directory for the required files and playlists. It can be any directory for which you have read and write access, e.g. {{ic|~/.config/mpd/}} or {{ic|~/.mpd/}}. This section assumes it is {{ic|~/.config/mpd/}}, which corresponds to the default value of {{ic|$XDG_CONFIG_HOME}} (part of [http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html XDG Base Directory Specification]).
{{hc|~/.xinitrc|
+
#run systemd as user instance
+
systemd --user &
+
}}
+
  
Log out, log in your xsession.
+
MPD searches for a config file in {{ic|$XDG_CONFIG_HOME/mpd/mpd.conf}} and then {{ic|~/.mpdconf}}. It is also possible to pass other path as command line argument.
Let's first check mpd is not running. If yes, just kill it. If not, continue to the next step.
+
{{bc|$ ps -ef &#124; grep mpd}}
+
  
Now, enable and start mpd.service as per user
+
Copy the example configuration file to desired location, for example:
$ systemctl --user enable mpd
+
$ systemctl --user start mpd
+
  
Check the mpd status and see if mpd.service is correctly enabled and started
+
$ mkdir -p ~/.config/mpd
{{bc|$ systemctl --user status mpd}}
+
$ cp /usr/share/doc/mpd/mpdconf.example ~/.config/mpd/mpd.conf
  
==== Scripted Configuration ====
+
Edit {{ic|~/.config/mpd/mpd.conf}} and specify the required files:
Rasi has written a script that will create the proper directory structure, configuration files and prompt for the location of the user's Music directory; it can be downloaded [http://53280.de/dl/mpdsetup.sh here].
+
  
=== Global Configuration ===
+
{{hc|~/.config/mpd/mpd.conf|
{{Warning|Users of PulseAudio with a local mpd have to implement a [[Music Player Daemon/Tips and Tricks#Local (with separate mpd user)|workaround]] in order to run mpd as its own user!}}
+
# Required files
 +
db_file            "~/.config/mpd/database"
 +
log_file          "~/.config/mpd/log"
  
The default Arch install keeps the setup in {{ic|/var/lib/mpd}} and uses ''mpd'' as default user.
+
# Optional
 
+
music_directory   "~/Music"
Edit {{ic|/etc/mpd.conf}} to reflect as such:
+
playlist_directory "~/.config/mpd/playlists"
{{hc|/etc/mpd.conf|
+
pid_file          "~/.config/mpd/pid"
music_directory       "/path/to/music/dir"
+
state_file        "~/.config/mpd/state"
playlist_directory   "/var/lib/mpd/playlists"
+
sticker_file      "~/.config/mpd/sticker.sql"
db_file              "/var/lib/mpd/mpd.db"
+
log_file              "syslog"
+
pid_file              "/run/mpd/mpd.pid"
+
state_file            "/var/lib/mpd/mpdstate"
+
user                  "mpd"
+
 
}}
 
}}
  
We just configured MPD to run as the ''mpd'' user, but {{ic|/var/lib/mpd}} is owned by ''root'' by default, we need to change this so ''mpd'' can write here:
+
Create the playlist directory as configured above:
# chown -R mpd /var/lib/mpd
+
  
==== Music directory ====
+
$ mkdir ~/.config/mpd/playlists
MPD needs to have {{ic|+x}} permissions on ''all'' parent directories to the music collection (ie. if it's located outside of {{ic|/var/lib/mpd}}). Thus users will most likely need to remount the music directory under a directory that mpd has access to -- this only applies if running as the 'mpd' user.
+
  
# mkdir /var/lib/mpd/music
+
When the paths of required files are configured, MPD can be started. To specify custom location of the configuration file:
# echo "/path/to/music/dir /var/lib/mpd/music none bind" >> /etc/fstab
+
# mount -a
+
Also see [https://bbs.archlinux.org/viewtopic.php?id=86449 this forum thread].
+
  
An additional solution would be to just create a symbolic link into {{ic|/var/lib/mpd/music}}.
+
  $ mpd ''config_file''
# mkdir /var/lib/mpd/music
+
  # ln -s /path/to/music/dir /var/lib/mpd/music/
+
  
If the music collection is contained under multiple directories, create symbolic links under the main music directory in {{ic|/var/lib/mpd}}. Remember to set permissions accordingly on the directories being linked.
+
==== Autostart on tty login ====
  
==== Start MPD ====
+
To start MPD on login add the following to {{ic|~/.profile}} (or another [[Autostarting#Shells|autostart file]]):
MPD can be controlled with the ''mpd'' [[daemon]]. The first startup can take some time as MPD will scan your music directory.
+
  
Test everything by starting a client application ({{Pkg|ncmpc}} is a light and easy to use client), and play some music!
+
# MPD daemon start (if no other user instance exists)
 +
[ ! -s ~/.config/mpd/pid ] && mpd
  
==== Configure audio ====
+
==== Autostart in X ====
{{Note|Everything should work by default, only change this if it doesn't work for you!}}
+
To change the volume for mpd independent from other programs, uncomment or add this switch in mpd.conf:
+
{{hc|/etc/mpd.conf|
+
mixer_type "software"
+
}}
+
  
Users of [[ALSA]] will want to have the following device definition, which allows software volume control in the MPD client to control the volume separately from other applications.
+
If you use a [[desktop environment]], place the following file in {{ic|~/.config/autostart/}}:
{{hc|/etc/mpd.conf|2=
+
{{hc|~/.config/autostart/mpd.desktop|<nowiki>
audio_output {
+
[Desktop Entry]
        type            "alsa"
+
Encoding=UTF-8
        name            "My Sound Card"
+
Type=Application
        mixer_type      "software"      # optional
+
Name=Music Player Daemon
}
+
Comment=Server for playing audio files
}}
+
Exec=mpd
 +
StartupNotify=false
 +
Terminal=false
 +
Hidden=false
 +
X-GNOME-Autostart-enabled=false
 +
</nowiki>}}
  
Users of [[PulseAudio]] will need to make the following modification:
+
If you do not use a DE, place the line from [[#Autostart on tty login]] in your [[Autostarting#Graphical|autostart file]].
{{hc|/etc/mpd.conf|2=
+
audio_output {
+
        type            "pulse"
+
        name            "pulse audio"
+
}
+
}}
+
  
==== Changing user ====
+
==== Autostart with systemd ====
{{Note|This is only required if you change the user!}}
+
Changing the group that MPD runs as may result in errors like "output: Failed to open "My ALSA Device"" "[alsa]: Failed to open ALSA device "default": No such file or directory" "player_thread: problems opening audio device while playing "Song Name.mp3""
+
  
This is because by default MPD runs as member of '''audio''' group and the sound devices under {{Ic|/dev/snd/}} are owned by this group, so add user {{Ic|mpd}} to group {{Ic|audio}}.
+
{{Note|It is assumed that you already have systemd user-session manager running (which should be default). See the [[systemd/User]] page for details.}}
# gpasswd -a mpd audio
+
  
==== Timeline of MPD startup ====
+
The package {{Pkg|mpd}} provides user service file in {{ic|/usr/lib/systemd/user/mpd.service}}. The configuration file is expected to exist either in {{ic|~/.mpdconf}} or {{ic|~/.config/mpd/mpd.conf}}, see [[systemd#Editing provided units]] if you would like to use different path. The process is not started as root, so you should not use the {{ic|user}} and {{ic|group}} variables in the MPD configuration file, the process already has user permissions and therefore it is not necessary to change them further.
To depict when MPD drops its superuser privileges and assumes those of the user set in the configuration, the timeline of a normal MPD startup is listed here:
+
  
# Since MPD is started as root by systemd, it first reads the {{ic|/etc/mpd.conf}} file.
+
All you have to do is enable and start the {{ic|mpd}} [[systemd/User#User Services|user service]].
# MPD reads the user variable in the {{ic|/etc/mpd.conf}} file, and changes from root to this user.
+
# MPD then reads the contents of the {{ic|/etc/mpd.conf}} file and configures itself accordingly.
+
  
Notice that MPD changes the running user from root to the one named in the {{ic|/etc/mpd.conf}} file.  
+
{{Note|
This way, uses of {{ic|~}} in the configuration file point correctly to the home user's directory, and not root's directory.  
+
* {{Pkg|mpd}} provides also system service file in {{ic|/usr/lib/systemd/system/mpd.service}}, but as the process is started as root, it does not read the user configuration file and falls back to {{ic|/etc/mpd.conf}}. [[#Global configuration|Global configuration]] is described in other section.
It may be worthwhile to change all uses of {{ic|~}} to {{ic|/home/username}} to avoid any confusion over this aspect of MPD's behavior.
+
* Make sure to disable every other method of starting mpd you used before.
 +
}}
 +
 
 +
==== Scripted configuration ====
 +
 
 +
You can use a [https://gist.githubusercontent.com/kurobeats/1c92c0a43f93ba49d798/raw/3a2661f071bcd3ed0b8c8c6560fc20a723dbbc45/mpdsetup.sh script] to create the proper directory structure, configuration files and prompt for the location of the user's Music directory.
 +
 
 +
==== Scripted configuration for bit perfect playback ====
 +
 
 +
You can use a [http://lacocina.nl/audiophile-mpd bash script] to also create a valid mpd configuration file which focusses on bit perfect audio playback. That is playback  without any resampling or format conversion. It does this by setting audio output parameters to use a direct alsa hwardware address (like `hw:0,0`). The script detects and lists which playback interfaces alsa supports. When one interface is found it uses that one, if multiple are found it prompts the user which one to use. When not specified on the command line, it auto configures things like the music_directory and mpd's home directory by using freedesktop.org XDG configuration.
  
 
=== Multi-mpd setup ===
 
=== Multi-mpd setup ===
'''Useful if running an icecast server.'''
+
 
 +
==== Running an icecast server ====
  
 
For a second MPD (e.g., with icecast output to share music over the network) using the same music and playlist as the one above, simply copy the above configuration file and make a new file (e.g., {{ic|/home/username/.mpd/config-icecast}}), and only change the log_file, error_file, pid_file, and state_file parameters (e.g., {{ic|mpd-icecast.log}}, {{ic|mpd-icecast.error}}, and so on); using the same directory paths for the music and playlist directories would ensure that this second mpd would use the same music collection as the first one e.g., creating and editing a playlist under the first daemon would affect the second daemon as well.  Users do not have to create the same playlists all over again for the second daemon.  Call this second daemon the same way from {{ic|~/.xinitrc}} above. (Just be sure to have a different port number, so as to not conflict with the first mpd daemon).
 
For a second MPD (e.g., with icecast output to share music over the network) using the same music and playlist as the one above, simply copy the above configuration file and make a new file (e.g., {{ic|/home/username/.mpd/config-icecast}}), and only change the log_file, error_file, pid_file, and state_file parameters (e.g., {{ic|mpd-icecast.log}}, {{ic|mpd-icecast.error}}, and so on); using the same directory paths for the music and playlist directories would ensure that this second mpd would use the same music collection as the first one e.g., creating and editing a playlist under the first daemon would affect the second daemon as well.  Users do not have to create the same playlists all over again for the second daemon.  Call this second daemon the same way from {{ic|~/.xinitrc}} above. (Just be sure to have a different port number, so as to not conflict with the first mpd daemon).
 +
 +
==== Satellite setup ====
 +
 +
The method above works, but at least in theory could lead to issues with the database, when both mpd instances try to write to the same database file. MPD has a [http://www.musicpd.org/doc/user/advanced_config.html#satellite satellite mode] where one instance can receive the database from an already running mpd instance.
 +
 +
in your config-icecast add this, where host and port reflect your primary mpd server.
 +
 +
{{bc|
 +
database {
 +
    plugin "proxy"
 +
    host "localhost"
 +
    port "6600"
 +
}
 +
}}
  
 
== Clients ==
 
== Clients ==
A separate client is needed to control mpd. Popular options are:
+
 
 +
A separate client is needed to control mpd. See a long list of clients at the [http://mpd.wikia.com/wiki/Clients mpd wiki]. Popular options are:
  
 
=== Console ===
 
=== Console ===
*{{App|mpc|Simple KISS client. All basic functionality available|http://mpd.wikia.com/wiki/Client:Mpc|{{Pkg|mpc}}}}
+
 
*{{App|ncmpc|A NCurses client for mpd|http://mpd.wikia.com/wiki/Client:Ncmpc|{{Pkg|ncmpc}}}}
+
*{{App|mpc|Command line user interface for MPD server|http://www.musicpd.org/clients/mpc/|{{Pkg|mpc}}}}
*{{App|[[ncmpcpp]]|An almost exact clone of ncmpc with some new features written in C++ (tag editor, search engine)|http://unkart.ovh.org/ncmpcpp/|{{Pkg|ncmpcpp}}}}
+
*{{App|ncmpc|Ncurses client for mpd|http://www.musicpd.org/clients/ncmpc/|{{Pkg|ncmpc}}}}
*{{App|pms|Highly configurable and accessible ncurses client|http://pms.sourceforge.net/|{{AUR|pmus}}}}
+
*{{App|[[ncmpcpp]]|Almost exact clone of ncmpc with some new features written in C++ (tag editor, search engine)|http://ncmpcpp.rybczak.net/|{{Pkg|ncmpcpp}}}}
*{{App|vimpc|ncurses based MPD client with vi-like key bindings|http://sourceforge.net/projects/vimpc/|{{AUR|vimpc}}}}
+
*{{App|pms|Highly configurable and accessible ncurses client|http://pms.sourceforge.net/|{{AUR|pmus-git}}}}
 +
*{{App|vimpc|Ncurses based MPD client with vi-like key bindings|http://sourceforge.net/projects/vimpc/|{{AUR|vimpc-git}}}}
  
 
=== Graphical ===
 
=== Graphical ===
*{{App|Ario|A very feature-rich GTK2 GUI client for mpd, inspired by Rhythmbox|http://ario-player.sourceforge.net/|{{Pkg|ario}}}}
 
*{{App|QmpdClient|A GUI client written with Qt 4.x|http://bitcheese.net/wiki/QMPDClient|{{Pkg|qmpdclient}}}}
 
*{{App|Sonata|An elegant Python GTK+ client|http://sonata.berlios.de/|{{Pkg|sonata}}}}
 
*{{App|gmpc|GNOME Client|http://gmpc.wikia.com/wiki/Gnome_Music_Player_Client|{{Pkg|gmpc}}}}
 
*{{App|Dmpc|Dmenu-based MPC client with a playlist manager and state-saving on playlist changes|http://wintervenom.mine.nu/|{{AUR|dmpc}}}}
 
*{{App|Cantata|High-feature, Qt4/KDE4 client for MPD with very configurable interface|https://code.google.com/p/cantata/|{{AUR|cantata-qt}}}}
 
=== Web ===
 
*{{App|Patchfork|web client for MPD written in PHP and Ajax|http://mpd.wikia.com/wiki/Client:Pitchfork|{{AUR|patchfork-git}}}}.
 
  
See a long list of clients at the [http://mpd.wikia.com/wiki/Clients mpd wiki].
+
*{{App|Ario|Very feature-rich GTK2 GUI client for mpd, inspired by Rhythmbox|http://ario-player.sourceforge.net/|{{Pkg|ario}}}}
 +
*{{App|QmpdClient|GUI client written with Qt 4.x|http://bitcheese.net/wiki/QMPDClient|{{Pkg|qmpdclient}}}}
 +
*{{App|Sonata|Elegant Python GTK+ client|http://www.nongnu.org/sonata/|{{Pkg|sonata}}}}
 +
*{{App|gmpc|GTK2 frontend for Music Player Daemon. It is designed to be lightweight and easy to use, while providing full access to all of MPD's features. Users are presented with several different methods to browse through their music. It can be extended by plugins, of which many are available.|http://gmpclient.org/|{{Pkg|gmpc}}}}
 +
*{{App|Cantata|High-feature, Qt4, Qt5 or KDE client for MPD with very configurable interface|https://github.com/CDrummond/cantata|{{Pkg|cantata}}}}
 +
 
 +
== See also ==
  
== External links ==
+
* [http://forum.musicpd.org/ MPD Forum]
* [http://mpd.wikia.com/wiki/Clients Sorted List of MPD Clients]
+
* [http://www.musicpd.org/doc/user/ MPD User Manual]
* [http://www.musicpd.org/forum/ MPD forum]
+
* [[Wikipedia:Music Player Daemon|Wikipedia article]]

Latest revision as of 12:31, 28 March 2016

Tango-edit-clear.pngThis article or section needs language, wiki syntax or style improvements.Tango-edit-clear.png

Reason: Needs improvements in structure and clarify; compare to the User's manual. (Discuss in Talk:Music Player Daemon#Style comment)

MPD (music player daemon) is an audio player that has a server-client architecture. It plays audio files, organizes playlists and maintains a music database all while using very few resources. In order to interface with it, a separate client is needed.

Installation

Install the mpd package, or mpd-gitAUR for the development version.

Note: An alternative plug-in based implementation called Mopidy exists. It is available as mopidy and mopidy-gitAUR. Be warned that is not a complete MPD drop-in replacement.

Setup

MPD is able to run locally (per user settings), globally (settings apply to all users), and in multiple instances. The way of setting up mpd depends on the way it is intended to be used: a local configuration may prove more useful on a desktop system, for example.

In order for MPD to be able to play back audio, ALSA or OSS (optionally with PulseAudio) needs to be setup and working.

MPD is configured in mpd.conf. The location of this file depends on how you want to run MPD (see the sections below). These are commonly used configuration options:

  • pid_file - The file where mpd stores its process ID
  • db_file - The music database
  • state_file - MPD's current state is noted here
  • playlist_directory - The folder where playlists are saved into
  • music_directory - The folder that MPD scans for music
  • sticker_file - The sticker database

Global configuration

Warning: Users of PulseAudio with a global mpd have to implement a workaround in order to run mpd as its own user!

The default /etc/mpd.conf keeps the setup in /var/lib/mpd which is assigned to user as well as primary group mpd.

Music directory

The music directory has to be set by parameter music_directory in file /etc/mpd.conf:

music_directory "/path/to/music"

MPD needs to have +x permissions on all parent directories to the music collection and also read access to all directories containing music files. This conflicts with the default configuration of the user directory where many users store their music.

While there are several solutions to this problem one of these should be most practical:

  • run MPD as user
  • add the mpd user to your login group and grant group permission to your user directory:
 # gpasswd -a mpd <your login group>
 $ chmod 710 /home/<your home dir>
  • put your music collection to a different path (a) by moving it entirely, (b) with a bind mount or (c) with a Btrfs subvolume (you should make this change persistent with an entry to /etc/fstab ). Permissions of alternate directories can be adjusted with Access Control Lists.

The MPD config must contain only one music directory. If the music collection is contained under multiple directories, create symbolic links under the main music directory in /var/lib/mpd. Remember to set permissions accordingly on the directories being linked.

Start MPD

MPD can be controlled with mpd.service using systemd. The first startup can take some time as MPD will scan your music directory.

Test everything by starting a client application (ncmpc is a light and easy to use client), and play some music!

Socket activation

If the mpd.socket unit (provided by mpd) is enabled while mpd.service is disabled, systemd will not start mpd immediately, but it will listen on the appropriate sockets. When an mpd client attempts to connect on one of those sockets, systemd will start mpd.service and transparently hand over control of those ports to the mpd process.

If you prefer to listen on different UNIX sockets or network ports (even multiple sockets of each type), or if you prefer not to listen on network ports at all, edit the mpd.socket unit appropriately and modify /etc/mpd.conf to match the configuration (see man 5 mpd.conf for details).

Configure audio

Users of ALSA will want to have the following device definition; replace My Sound Card with the name of a sound card or pcm (aplay --list-pcms).

/etc/mpd.conf
audio_output {
        type            "alsa"
        name            "My Sound Card"
        mixer_type      "software"      # optional
}

The mixer_type "software" option tells 'mpd' to use its own independent software volume control.

Users of PulseAudio will need to make the following modification:

/etc/mpd.conf
audio_output {
        type            "pulse"
        name            "pulse audio"
}

PulseAudio supports multiple advanced operations, e.g. transferring the audio to a different machine. For advanced configuration with MPD see Music Player Daemon Community Wiki.

Changing user

Changing the group that MPD runs as may result in errors like output: Failed to open "My ALSA Device", [alsa]: Failed to open ALSA device "default": No such file or directory or player_thread: problems opening audio device while playing "Song Name.mp3".

This is because the MPD users need to be part of the audio group to access sound devices under /dev/snd/. To fix it add user make the MPD user part of the audio group:

# gpasswd -a mpd audio

Timeline of MPD startup

To depict when MPD drops its superuser privileges and assumes those of the user set in the configuration, the timeline of a normal MPD startup is listed here:

  1. Since MPD is started as root by systemd, it first reads the /etc/mpd.conf file.
  2. MPD reads the user variable in the /etc/mpd.conf file, and changes from root to this user.
  3. 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.

Local configuration (per user)

MPD can be configured per user (rather than the typical method of configuring MPD globally). Running MPD as a normal user has the benefits of:

  • A single directory ~/.config/mpd/ (or any other directory under $HOME) that will contain all the MPD configuration files.
  • Easier to avoid unforeseen read/write permission errors.

Good practice is to create a single directory for the required files and playlists. It can be any directory for which you have read and write access, e.g. ~/.config/mpd/ or ~/.mpd/. This section assumes it is ~/.config/mpd/, which corresponds to the default value of $XDG_CONFIG_HOME (part of XDG Base Directory Specification).

MPD searches for a config file in $XDG_CONFIG_HOME/mpd/mpd.conf and then ~/.mpdconf. It is also possible to pass other path as command line argument.

Copy the example configuration file to desired location, for example:

$ mkdir -p ~/.config/mpd
$ cp /usr/share/doc/mpd/mpdconf.example ~/.config/mpd/mpd.conf

Edit ~/.config/mpd/mpd.conf and specify the required files:

~/.config/mpd/mpd.conf
# Required files
db_file            "~/.config/mpd/database"
log_file           "~/.config/mpd/log"

# Optional
music_directory    "~/Music"
playlist_directory "~/.config/mpd/playlists"
pid_file           "~/.config/mpd/pid"
state_file         "~/.config/mpd/state"
sticker_file       "~/.config/mpd/sticker.sql"

Create the playlist directory as configured above:

$ mkdir ~/.config/mpd/playlists

When the paths of required files are configured, MPD can be started. To specify custom location of the configuration file:

$ mpd config_file

Autostart on tty login

To start MPD on login add the following to ~/.profile (or another autostart file):

# MPD daemon start (if no other user instance exists)
[ ! -s ~/.config/mpd/pid ] && mpd

Autostart in X

If you use a desktop environment, place the following file in ~/.config/autostart/:

~/.config/autostart/mpd.desktop
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=Music Player Daemon
Comment=Server for playing audio files
Exec=mpd
StartupNotify=false
Terminal=false
Hidden=false
X-GNOME-Autostart-enabled=false

If you do not use a DE, place the line from #Autostart on tty login in your autostart file.

Autostart with systemd

Note: It is assumed that you already have systemd user-session manager running (which should be default). See the systemd/User page for details.

The package mpd provides user service file in /usr/lib/systemd/user/mpd.service. The configuration file is expected to exist either in ~/.mpdconf or ~/.config/mpd/mpd.conf, see systemd#Editing provided units if you would like to use different path. The process is not started as root, so you should not use the user and group variables in the MPD configuration file, the process already has user permissions and therefore it is not necessary to change them further.

All you have to do is enable and start the mpd user service.

Note:
  • mpd provides also system service file in /usr/lib/systemd/system/mpd.service, but as the process is started as root, it does not read the user configuration file and falls back to /etc/mpd.conf. Global configuration is described in other section.
  • Make sure to disable every other method of starting mpd you used before.

Scripted configuration

You can use a script to create the proper directory structure, configuration files and prompt for the location of the user's Music directory.

Scripted configuration for bit perfect playback

You can use a bash script to also create a valid mpd configuration file which focusses on bit perfect audio playback. That is playback without any resampling or format conversion. It does this by setting audio output parameters to use a direct alsa hwardware address (like `hw:0,0`). The script detects and lists which playback interfaces alsa supports. When one interface is found it uses that one, if multiple are found it prompts the user which one to use. When not specified on the command line, it auto configures things like the music_directory and mpd's home directory by using freedesktop.org XDG configuration.

Multi-mpd setup

Running an icecast server

For a second MPD (e.g., with icecast output to share music over the network) using the same music and playlist as the one above, simply copy the above configuration file and make a new file (e.g., /home/username/.mpd/config-icecast), and only change the log_file, error_file, pid_file, and state_file parameters (e.g., mpd-icecast.log, mpd-icecast.error, and so on); using the same directory paths for the music and playlist directories would ensure that this second mpd would use the same music collection as the first one e.g., creating and editing a playlist under the first daemon would affect the second daemon as well. Users do not have to create the same playlists all over again for the second daemon. Call this second daemon the same way from ~/.xinitrc above. (Just be sure to have a different port number, so as to not conflict with the first mpd daemon).

Satellite setup

The method above works, but at least in theory could lead to issues with the database, when both mpd instances try to write to the same database file. MPD has a satellite mode where one instance can receive the database from an already running mpd instance.

in your config-icecast add this, where host and port reflect your primary mpd server.

database {
    plugin "proxy"
    host "localhost"
    port "6600"
}

Clients

A separate client is needed to control mpd. See a long list of clients at the mpd wiki. Popular options are:

Console

  • mpc — Command line user interface for MPD server
http://www.musicpd.org/clients/mpc/ || mpc
  • ncmpc — Ncurses client for mpd
http://www.musicpd.org/clients/ncmpc/ || ncmpc
  • ncmpcpp — Almost exact clone of ncmpc with some new features written in C++ (tag editor, search engine)
http://ncmpcpp.rybczak.net/ || ncmpcpp
  • pms — Highly configurable and accessible ncurses client
http://pms.sourceforge.net/ || pmus-gitAUR
  • vimpc — Ncurses based MPD client with vi-like key bindings
http://sourceforge.net/projects/vimpc/ || vimpc-gitAUR

Graphical

  • Ario — Very feature-rich GTK2 GUI client for mpd, inspired by Rhythmbox
http://ario-player.sourceforge.net/ || ario
  • QmpdClient — GUI client written with Qt 4.x
http://bitcheese.net/wiki/QMPDClient || qmpdclient
  • Sonata — Elegant Python GTK+ client
http://www.nongnu.org/sonata/ || sonata
  • gmpc — GTK2 frontend for Music Player Daemon. It is designed to be lightweight and easy to use, while providing full access to all of MPD's features. Users are presented with several different methods to browse through their music. It can be extended by plugins, of which many are available.
http://gmpclient.org/ || gmpc
  • Cantata — High-feature, Qt4, Qt5 or KDE client for MPD with very configurable interface
https://github.com/CDrummond/cantata || cantata

See also