# Music Player Daemon (简体中文)

相关文章

- MPD/Tips and Tricks
- MPD/Troubleshooting

翻译状态： 本文是英文页面 Music_Player_Daemon 的翻译，最后翻译时间：2013-11-29`，点击这里可以查看翻译后英文页面的改动。

MPD (music player daemon) 是一个服务器-客户端架构的音频播放器. 它使用很少的系统资源播放音频文件, 管理播放列表和维护一个音乐数据库. 你需要一个独立的客户端 与它进行交互

## 安装

### 稳定版

官方软件仓库 提供 mpd 的最新稳定版。

### Git版

如果您想要安装最新的开发版， AUR 提供了几个选择， 比如, mpd-git。

## 设置

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:

- mpd.db - The music database
- mpd.pid - The file where mpd stores its process ID
- mpd.log - mpd logs here
- mpdstate - mpd's current state is noted here
- playlists - the folder where playlists are saved into

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

### 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 `~/.mpd` (or any other directory under `/home/$USER/`) that will contain all the MPD configuration files.
- 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:

    mkdir -p ~/.mpd/playlists
    cp /usr/share/doc/mpd/mpdconf.example ~/.mpd/mpd.conf
    touch ~/.mpd/{database,log,state}

Edit `~/.mpd/mpd.conf` and specify the requisite files:

    ~/.mpd/mpd.conf

    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 `mpd` on the command line (mpd first searches for `~/.mpd.conf`, then `~/.mpd/mpd.conf`, and then for `/etc/mpd.conf` [there is no support for XDG-config directory `~/.config/mpd/mpd.conf`]). To specify the location of the configuration file :

    mpd ~/.config/mpd/mpd.conf

To start MPD on login add to `~/.profile` (or another Autostart file):

    # MPD daemon start (if no other user instance exists)
    [ ! -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 `~/.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

#### Start MPD per user with systemd

As recommended above, it is best to use MPD as a service per user. In this case, we will use `~/.mpd/mpd.conf` and not start mpd.service as a daemon for the whole system and all users. We will NOT use the `/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 `systemd --user`.

If you already enabled it, just disable it first:

     # systemctl disable mpd.service

If you used to start mpd inside your `~/.xinitrc`, comment or delete the line

    mpd ~/.mpdconf

Then, edit a new file ~/.config/systemd/user/mpd.service

    ~/.config/systemd/user/mpd.service

    [Unit]
    Description = Music Player Daemon

    [Service]
    ExecStart = /usr/bin/mpd --no-daemon
    Restart = always

    [Install]
    WantedBy = default.target

Then, add this line to .xinitrc before any exec <session-command>

    ~/.xinitrc

    #run systemd as user instance
    systemd --user &

Log out, log in your xsession. Let us first check mpd is not running. If yes, just kill it. If not, continue to the next step.

    $ ps -ef | grep mpd

Now, enable and start mpd.service as per user

    $ systemctl --user enable mpd
    $ systemctl --user start mpd

Check the mpd status and see if mpd.service is correctly enabled and started

    $ systemctl --user status mpd

#### Scripted Configuration

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 here.

### Global Configuration

Note: For user-specific permissions, change the "user" line above to reflect one of the system users.
Warning: Users of PulseAudio with a local mpd have to implement a workaround in order to run mpd as its own user!

MPD comes with an example configuration file, available at `/usr/share/doc/mpd/mpdconf.example`. This file holds an abundance of information on MPD configuration, and holds default mixer values.

Normally,`/etc/mpd.conf` is created when installing mpd. If not, copy the included example file to `/etc/mpd.conf`.

    # cp /usr/share/doc/mpd/mpdconf.example /etc/mpd.conf

#### Editing mpd.conf

The default Arch install keeps the setup in `/var/lib/mpd` and uses "mpd" as default user.

Edit `/etc/mpd.conf` to reflect as such:

    /etc/mpd.conf

    music_directory       "/home/user/music"
    playlist_directory    "/var/lib/mpd/playlists"
    db_file               "/var/lib/mpd/mpd.db"
    log_file              "/var/log/mpd/mpd.log"
    pid_file              "/run/mpd/mpd.pid"
    state_file            "/var/lib/mpd/mpdstate"
    user                  "mpd"
    # bind_to_address       "127.0.0.1"
    # port                  "6600"

Now change permissions for mpd to write to `/var/log/mpd/mpd.log`, otherwise mpd will return an error when it is started. The best way is to make the `/var/log/mpd/mpd.log` belong to the mpd user.

      # chown -R mpd /var/log/mpd

Run this to create the /run/mpd dir:

      # systemd-tmpfiles --create mpd.conf

To change the volume for mpd independent from other programs, uncomment or add this switch in mpd.conf:

    /etc/mpd.conf

    mixer_type			"software"

Users of PulseAudio will need to make the following modification:

    /etc/mpd.conf

    audio_output {
            type                    "pulse"
            name                    "pulse audio"
    }

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.

    /etc/mpd.conf

    audio_output {
            type            "alsa"
            name            "My Sound Card"
            mixer_type      "software"      # optional
    }

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 `/dev/snd/` are owned by this group, so add user `mpd` to group `audio`.

    # gpasswd -a mpd audio

##### Music directory

MPD needs to have `+x` permissions on all parent directories to the music collection (ie. if it's located outside of "mpd" home directory `/var/lib/mpd`). By default useradd sets permissions on home directories to `1700 drwx------`. 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
    # echo "/home/$USER/music /var/lib/mpd/music none bind" >> /etc/fstab
    # mount -a
    # systemctl restart mpd.service

Also see this forum thread.

An additional solution would be to just create a symbolic link into `/var/lib/mpd/music`.

    # mkdir /var/lib/mpd/music
    # ln -s MUSIC_FOLDER /var/lib/mpd/music/
    # systemctl restart mpd.service

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.

#### Creating the required files

Now, having finished configuring MPD, the files and directories for MPD to write in need to be created:

Create the directories and files specified in `/etc/mpd.conf`:

    # mkdir -p /var/lib/mpd/playlists
    # touch /var/lib/mpd/{mpd.db,mpdstate}

Usually the init-script should properly create `/run/mpd/` when starting. The daemon will use this directory to create `mpd.pid` in it. However, when running mpd as a different user, requires an update to the tmpfiles.d: copy `/usr/lib/tmpfiles.d/mpd.conf` to `/etc/tmpfiles.d/mpd.conf`: Change:

    d /run/mpd 0755 mpd mpd

to:

    d /run/mpd 0755 username groupname

Change the file's permissions so that the daemon can modify them.

    # chown -R mpd /var/lib/mpd

#### Create database

Creating the database is now accomplished via the update feature of the client, for example `mpc update`. You will need to install the mpc package for this. It is in the official repositories

Note: Creating the MPD database as root using `# mpd --create-db` is deprecated.

#### 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.

### Multi-mpd setup

Useful if 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).

## 客户端

需要安装独立客户端才能控制mpd。 常用的有这些：

### 命令行下

- mpc — 简单的基于KISS原则的客户端。 拥有所有的基本功能。

  http://mpd.wikia.com/wiki/Client:Mpc || mpc

- ncmpc — 一个使用NCursers的客户端。

  http://mpd.wikia.com/wiki/Client:Ncmpc || ncmpc

- ncmpcpp — 差不多是完全克隆ncmpc的客户端， 但是有一些用C++写成的新功能(tag editor, search engine)

  http://unkart.ovh.org/ncmpcpp/ || ncmpcpp

- pms — Highly configurable and accessible ncurses client

  http://pms.sourceforge.net/ || pmus

- vimpc — 基于ncurses的MPD客户端， 使用类似vi的快捷键

  http://sourceforge.net/projects/vimpc/ || vimpc

### 图形界面下

- Ario — A very feature-rich GTK2 GUI client for mpd, inspired by Rhythmbox

  http://ario-player.sourceforge.net/ || ario

- QmpdClient — A GUI client written with Qt 4.x

  http://bitcheese.net/wiki/QMPDClient || qmpdclient

- Sonata — An elegant Python GTK+ client

  http://sonata.berlios.de/ || sonata

- gmpc — GNOME Client

  http://gmpc.wikia.com/wiki/Gnome_Music_Player_Client || gmpc

- Dmpc — Dmenu-based MPC client with a playlist manager and state-saving on playlist changes

  http://wintervenom.mine.nu/ || dmpc

- Cantata — High-feature, Qt4/KDE4 client for MPD with very configurable interface

  https://code.google.com/p/cantata/ || cantata-qt

### Web界面

- Patchfork — web client for MPD written in PHP and Ajax

  http://mpd.wikia.com/wiki/Client:Pitchfork || patchfork-git.

See a long list of clients at the mpd wiki.

## External links

- Sorted List of MPD Clients
- MPD forum

Category:

- Player (简体中文)