# 为树莓派换上AirPlay 2

By [cylll](https://paragraph.com/@cylll) · 2023-03-23

---

这两天在Reddit上看到一个关于「AirPlay 2 on Linux」的帖子，其中推荐了 Shairport Sync 项目，于是我打算给家里在用的树莓派的AirPlay升级一下。大家有能力的可以直接去看[原文](https://github.com/mikebrady/shairport-sync/blob/development/AIRPLAY2.md)，里面文档写的很详细

准备
--

*   一台Linux 或 FreeBSD的设备，其性能至少要大于等于树莓派 2
    
*   319 和 320端口没有被占用
    
*   一个音箱（最好是3.5mm接口）
    

开始安装
----

首选建议大家先将系统更新到最新状态，可以大大减少许多莫名其妙的问题。

    sudo apt-get update #检查更新
    sudo apt-get upgrade #执行更新
    

### 关闭Wi-Fi节能管理

如果你使用设备使用Wi-Fi连接的话，需要将节能管理关闭，可以避免安装后设备进入节能后搜索不到音箱。

    iwconfig wlan0 power off
    

### 安装依赖

    apt install --no-install-recommends build-essential git xxd xmltoman \ autoconf automake libtool libpopt-dev libconfig-dev libasound2-dev \ avahi-daemon libavahi-client-dev libssl-dev libsoxr-dev libplist-dev \ libsodium-dev libavutil-dev libavcodec-dev libavformat-dev uuid-dev libgcrypt-dev
    

### nqptp

[nqptp](https://github.com/mikebrady/nqptp)是 **Shairport Sync** 监控时间的重要依赖，需要手动编译安装。

    git clone https://github.com/mikebrady/nqptp.git #下载项目
    cd nqptp #进入项目目录
    autoreconf -fi #编译
    ./configure --with-systemd-startup #配置
    make #构建环境
    make install #安装
    

安装完毕后，需要启动nqptp：

    systemctl enable nqptp #设置自动启动
    systemctl start nqptp #启动nqptp
    

### sps-alsa-explore

一个可以帮助我们Shairport Sync的工具，可以很直观的列出当前的音频输出设备，需要手动编译安装。

    git clone https://github.com/mikebrady/sps-alsa-explore.git #下载项目
    cd sps-alsa-explore #进入项目目录
    autoreconf -fi #编译
    ./configure #配置
    make #构建环境
    

将音箱连接到树莓派，然后在sps-alsa-explore目录下执行`./sps-alsa-explore`：

![](https://storage.googleapis.com/papyrus_images/87e1589cc2b365467f66658eaaa281d621e65e3c93a78838c63b40712a166db9.png)

记录下`Device Full Name:"hw:Headphones"`和`Possible mixers:"Headphone"` 在后续配置文件中使用。

### 编译安装 Shairport Sync

    git clone https://github.com/mikebrady/shairport-sync.git #下载项目
    cd shairport-sync  #进入项目目录
    git checkout development #切换到development分支
    autoreconf -fi #编译
    ./configure --sysconfdir=/etc --with-alsa --with-soxr --with-avahi --with-ssl=openssl --with-systemd --with-airplay-2 #配置
    make -j #构建环境
    make install #安装
    

### 配置 Shairport Sync

Shairport Sync 的配置文件路径在`/etc/shairport-sync.conf`，其中输出设备这里需要配置一下，熟悉vi/vim的朋友可以直接 `sudo vi /etc/shairport-sync.conf`配置一下，其中`output_device`和`mixer_control_name`都可以在前面`./sps-alsa-explore`的结果中找到。

    alsa =
    {
      output_device = "hw:Headphones";
      mixer_control_name = "Headphone";
    };
    

配置文件中有大量注释说明，大家可以按需配置。

### 启动Shairport Sync

    systemctl enable shairport-sync #设置自动启动
    systemctl start shairport-sync #启动Shairport Sync
    

添加到HomeKit
----------

在添加配件时，点击「没有代码或无法扫描？」，家庭 app 就会发现 AirPlay 2 设备，点击即可加入。

![添加到HomeKit](https://storage.googleapis.com/papyrus_images/178566b477fc32c931ca4809d25f2d26c8e5cd0114b71b5bf6a7210e256dbe3f.png)

添加到HomeKit

遇到的问题
-----

在「多房间音频系统」播放时存在延迟，我翻了下**Shairport Sync**的Issues发现并不是所有人都存在这个问题，通常HDMI连接的设备都会存在100毫秒左右的延迟。在文档的常见问题中给出的解决方法是手动设置一个延迟，在配置文件`/etc/shairport-sync.conf`中有一个`audio_backend_latency_offset_in_seconds`配置项单位为秒：

![audio_backend_latency_offset_in_seconds配置](https://storage.googleapis.com/papyrus_images/1bac1d594002776d96882015725dc9f02fa2324d4652b988d3d2f365c72bb6e4.png)

audio\_backend\_latency\_offset\_in\_seconds配置

有其他问题的话可以先到文档中排查 [「Troubleshooting」](https://github.com/mikebrady/shairport-sync/blob/master/TROUBLESHOOTING.md)

使用感受
----

折腾完用了几天后感觉还算是可用状态，借此也让我体验到「多房间音频系统」的功能，但仅限于听歌或播客之类的音频，AirPlay的延迟一就是个头疼的问题，许多视频软件由于没有适配会出现音画不同步问题，就算适配AirPlay之后也会存在一个缓冲的延迟。但作为音频播放器在「多房间音频系统」时还是很方便的。

![多房间音频系统](https://storage.googleapis.com/papyrus_images/beb329be61ff51f6c9a22fabbff4f91ee2802d8560c02a2f71048c50b6efdc7f.png)

多房间音频系统

当然对于不喜欢折腾的人，[Airport Express 2 (A1392)](https://support.apple.com/kb/sp651?locale=zh_CN) 也许是更好的选择，简单配置就可以让音箱支持AirPlay2，咸鱼价格在150～200。

![Airport Express 2 (A1392)](https://storage.googleapis.com/papyrus_images/ab7e9675cbd3ed674b6482e18e491a3392670281605f11591401bf1c8c213691.jpg)

Airport Express 2 (A1392)

参考资料
----

*   [「Shairport-Sync 」](https://github.com/mikebrady/shairport-sync)
    
*   [「AirPort Express 802.11n (第2代) 」](https://support.apple.com/kb/sp651?locale=zh_CN)

---

*Originally published on [cylll](https://paragraph.com/@cylll/airplay-2)*
