使用 Google Nest Mini 串流 AirPlay 藉由 Raspberry Pi

意外發現一個很好用的程式 AirConnect ,剛好家裡有兩台 Nest Mini 還有 raspberry pi 就可以用這樣的夢幻組合,把 Google Nest Mini 變成可以透過 AirPlay 串流音樂。

可以先瀏覽 AirConnect 使用已經發佈的版本:https://github.com/philippe44/AirConnect/releases

除了用 raspberry pi 之外還可以用其他版本的 linux, mac 甚至是 windows 只啟動服務要一直開機著。這篇文章就使用 raspberry pi 為例,先下載預先建立好的 aircast-linux-aarch64 : https://github.com/philippe44/AirConnect/blob/master/bin/aircast-linux-aarch64

$ sudo mkdir -p /var/lib/airconnect
$ cd /var/lib/airconnect
$ sudo curl -O https://github.com/philippe44/AirConnect/blob/master/bin/aircast-linux-aarch64
$ sudo chmod 755 aircast-linux-aarch64

可以先用 ./aircast-linux-aarch64 執行確認一下是否有抓到 Google Nest Mini,而我的狀態因為 raspberry pi 有兩個網段,所以要指定綁定在哪個網段。如果沒有別的網段就可以忽略 -b 的選項。

$ sudo ./aircast-linux-aarch64 -b wlan0

用上述的指令,就可以啟動服務,但是還沒設定成背景執行。 先用 Ctrl + C 跳出來。然後產生 config 檔案,用下列指令:

$ sudo ./aircast-linux-aarch64 -b wlan0 -i aircast.xml
[11:22:43.662168] main:912 Starting aircast version: v1.1.5 (May  7 2023 @ 13:42:19)
[11:22:43.662485] main:919 no config file, using defaults
[11:22:43.663259] Start:679 Binding to 192.168.0.20
[11:22:43.670813] Start:704 Starting pico HTTP server on port 39657
[11:22:44.123792] AddCastDevice:630 [0x104c7b30]: creating MAC
[11:22:44.123976] AddCastDevice:644 [0x104c7b30]: adding renderer (Google Nest Mini Right) with mac CCCCXXXXXXXX
[11:22:44.137688] AddCastDevice:630 [0x104c84d0]: creating MAC
[11:22:44.137768] AddCastDevice:644 [0x104c84d0]: adding renderer (Google Nest Mini) with mac CCCCXXXXXXXX
[11:22:44.138855] AddCastDevice:630 [0x104c8e70]: creating MAC
[11:22:44.138923] AddCastDevice:644 [0x104c8e70]: adding renderer (70PUH8215/96) with mac CCCCXXXXXXXX
[11:22:44.140082] AddCastDevice:630 [0x104c9810]: creating MAC
[11:22:44.140147] AddCastDevice:644 [0x104c9810]: adding renderer (Google Nest Mini Left) with mac CCCCXXXXXXXX
[11:22:44.141227] AddCastDevice:630 [0x104ca1b0]: creating MAC
[11:22:44.141291] AddCastDevice:644 [0x104ca1b0]: adding renderer (Chromecast (Google TV)) with mac CCCCXXXXXXXX
[11:23:04.672294] DeleteCastDevice:379 [0x104c7b30]: Cast device stopped
[11:23:04.673146] DeleteCastDevice:379 [0x104c84d0]: Cast device stopped
[11:23:04.674013] DeleteCastDevice:379 [0x104c8e70]: Cast device stopped
[11:23:04.674795] DeleteCastDevice:379 [0x104c9810]: Cast device stopped
[11:23:04.675372] DeleteCastDevice:379 [0x104ca1b0]: Cast device stopped

執行完就會產生 aircast.xml 檔案,看是否需要編輯 aircast.xml。因為如果不編輯他會把 Nest Mini 左右邊顯示,且會在每個裝置後面加一個 + 號,所以我選擇編輯 aircast.xml ,把它自訂成想要的名字及隱藏左右邊的 Nest Mini ,只顯示 左右兩邊 Group 起來的 Nest MIni。

接下來就可以建立服務讓他在背景執行,所以要寫一個服務描述檔案:

$ sudo vi /etc/systemd/system/aircast.service
[Unit]
Description=AirCast
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/var/lib/airconnect/aircast-linux-aarch64 -b wlan0 -l -z -x /var/lib/airconnect/aircast.xml
Restart=on-failure
RestartSec=30

[Install]
WantedBy=multi-user.target

然後讓他自動啟動,使用下列指令:

$ sudo systemctl enable aircast
Created symlink /etc/systemd/system/multi-user.target.wants/aircast.service → /etc/systemd/system/aircast.service.
$ sudo systemctl start aircast

再來就可以用 status 選項來檢查執行狀態

$ sudo systemctl status aircast
● aircast.service - AirCast
     Loaded: loaded (/etc/systemd/system/aircast.service; enabled; vendor prese>
     Active: active (running) since Sun 2023-06-18 11:44:40 CST; 1min 8s ago
   Main PID: 51473 (aircast-linux-a)
      Tasks: 17 (limit: 4430)
     Memory: 2.1M
        CPU: 1min 8.894s
     CGroup: /system.slice/aircast.service
             └─51473 /var/lib/airconnect/aircast-linux-aarch64 -b wlan0 -l -z ->

Jun 18 11:44:40 fuji systemd[1]: Started AirCast.
Jun 18 11:44:40 fuji aircast-linux-aarch64[51473]: [11:44:40.781795] main:912 S>
Jun 18 11:44:40 fuji aircast-linux-aarch64[51473]: [11:44:40.782558] Start:679 >
Jun 18 11:44:40 fuji aircast-linux-aarch64[51473]: [11:44:40.788615] Start:704 >
Jun 18 11:44:41 fuji aircast-linux-aarch64[51473]: [11:44:41.101260] AddCastDev>
Jun 18 11:44:41 fuji aircast-linux-aarch64[51473]: [11:44:41.108714] AddCastDev>
Jun 18 11:44:41 fuji aircast-linux-aarch64[51473]: [11:44:41.306149] AddCastDev>
lines 1-17/17 (END)

最後就拿手機來驗證一下是否能正常播放