发表于 2026年08月09日

armbian安装MPD添加DAC切换记录

结构:

/usr/local/bin/
 ├── mpd-autostart.sh       (你的MPD启动)
 └── mpd-manager.py         (网页DAC管理)

/etc/
 └── mpd.conf               (保持不动)

你的 /etc/mpd.conf 不用改,继续使用现在的:

 hw:1,0
My BurrBrown DAC
  1. 安装依赖 执行
apt update
apt install python3-flask alsa-utils -y

2、更新 MPD 启动脚本

编辑:

nano /usr/local/bin/mpd-autostart.sh

完整替换:

#!/bin/bash

mkdir -p /var/log/mpd
mkdir -p /run/mpd
mkdir -p /var/lib/mpd/playlists
mkdir -p /var/lib/mpd/cache
mkdir -p /var/lib/mpd/tag_cache
mkdir -p /media/music


chown -R mpd:audio \
/var/log/mpd \
/run/mpd \
/var/lib/mpd \
/media/music


chmod -R 775 \
/var/log/mpd \
/run/mpd \
/var/lib/mpd \
/media/music



killall -9 mpd 2>/dev/null

rm -f /run/mpd/pid


sleep 2



sudo -u mpd /usr/bin/mpd /etc/mpd.conf


sleep 5



export MPD_HOST="127.0.0.1"
export MPD_PORT="6600"



mpc update || true


sleep 2


mpc clear || true

mpc listall | mpc add || true



mpc repeat on

mpc random off

mpc volume 100



amixer sset Master 100% unmute 2>/dev/null || true

amixer sset PCM 100% unmute 2>/dev/null || true

amixer sset Line 100% unmute 2>/dev/null || true



mpc play 1 || true
保存:
chmod +x /usr/local/bin/mpd-autostart.sh
3、创建 DAC 管理网页

创建:

nano /usr/local/bin/mpd-manager.py
复制:
#!/usr/bin/env python3

from flask import Flask,request,redirect,render_template_string
import subprocess
import re
import shutil
import os


app=Flask(__name__)


MPD_CONF="/etc/mpd.conf"

START="/usr/local/bin/mpd-autostart.sh"


HTML="""

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>MPD DAC管理</title>

<style>

body{
background:#111;
color:#fff;
font-family:Arial;
padding:20px;
}

.box{
background:#222;
padding:20px;
border-radius:12px;
margin-bottom:20px;
}

button,select{
font-size:18px;
padding:10px;
width:100%;
}

</style>

</head>


<body>


<h2>CM101H MPD DAC管理</h2>


<div class="box">

<h3>当前配置</h3>

<pre>
{{current}}
</pre>

</div>



<div class="box">

<h3>检测到DAC</h3>


<form method="post" action="/set">


<select name="dac">


{% for d in dacs %}

<option value="{{d.hw}}">
{{d.hw}}  {{d.name}}

</option>


{% endfor %}


</select>


<br><br>


<button>
切换DAC
</button>


</form>


</div>



<div class="box">


<a href="/restart">

<button>
重启MPD
</button>

</a>


</div>


</body>

</html>

"""



def run(cmd):

    try:

        return subprocess.check_output(
        cmd,
        shell=True,
        stderr=subprocess.STDOUT
        ).decode()

    except:

        return ""



def scan_dac():

    out=run("aplay -l")

    result=[]


    for line in out.splitlines():

        m=re.search(
        r"card (\d+): (.*?) \[.*?\], device (\d+):",
        line
        )


        if m:

            result.append(
            {
            "hw":
            "hw:%s,%s"%(m.group(1),m.group(3)),

            "name":
            m.group(2)
            }
            )


    return result



def change_dac(hw,name):


    text=open(MPD_CONF).read()



    if not os.path.exists(
    "/etc/mpd.conf.backup"
    ):

        shutil.copy(
        MPD_CONF,
        "/etc/mpd.conf.backup"
        )


    start=text.find("audio_output {")


    if start<0:
        return



    count=0
    end=start


    for i in range(start,len(text)):

        if text[i]=="{":
            count+=1

        elif text[i]=="}":

            count-=1

            if count==0:
                end=i+1
                break



    new=f'''
audio_output {{

    type            "alsa"

    name            "{name}"

    device          "{hw}"

    mixer_type      "none"

    auto_resample   "no"

    auto_channels   "no"

    auto_format     "no"

}}
'''


    text=text[:start]+new+text[end:]


    open(
    MPD_CONF,
    "w"
    ).write(text)




@app.route("/")

def index():

    return render_template_string(
    HTML,
    dacs=scan_dac(),
    current=run(
    "grep -A12 audio_output /etc/mpd.conf"
    )
    )




@app.route("/set",methods=["POST"])

def setdac():

    hw=request.form["dac"]


    name="USB DAC"


    for d in scan_dac():

        if d["hw"]==hw:

            name=d["name"]


    change_dac(hw,name)


    subprocess.Popen(
    START
    )


    return redirect("/")




@app.route("/restart")

def restart():

    subprocess.Popen(
    START
    )

    return redirect("/")




app.run(
host="0.0.0.0",
port=8090
)
保存:
chmod +x /usr/local/bin/mpd-manager.py
4、设置网页后台开机启动

创建:

nano /etc/systemd/system/mpd-manager.service
内容:
[Unit]
Description=MPD Web Manager
After=network.target


[Service]

ExecStart=/usr/bin/python3 /usr/local/bin/mpd-manager.py

Restart=always


[Install]

WantedBy=multi-user.target
启动:
systemctl daemon-reload

systemctl enable mpd-manager

systemctl start mpd-manager
访问:
http://CM101H_IP:8090
会看到:
当前:
My BurrBrown DAC
hw:1,0


检测到:

hw:0,0 S905X-P212

hw:1,0 USB AUDIO DAC
选择后:

自动:

修改mpd.conf
↓
运行 mpd-autostart.sh
↓
MPD重新启动
↓
myMPD继续播放
这个版本是按照你现在的 CM101H 实际环境写的,不依赖 systemd 的 mpd.service,不会破坏你已经测试好的 SMB、USB挂载、myMPD。你直接按顺序复制即可。

延伸阅读