AirPlay on Raspberry Pi

把塵封已久的 Raspberry Pi 拿出來玩

為了使 raspberry pi 連 wifi 我買了 EDIMAX EW-7811Un 無線網卡.

首先, 先把 raspberry pi 的作業系統安裝好. 我是下載 RASPBIAN JESSIE LITE 比較不佔空間. 且我也沒有需要圖形介面.

安裝的過程如下 我是使用 mac 安裝的 :
1. 先找出 sd 卡是哪個 device

$ df -h
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk1 476Gi 392Gi 83Gi 83% 102853623 21872743 82% /
devfs 328Ki 328Ki 0Bi 100% 1136 0 100% /dev
map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net
map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /home
localhost:/qt1kkfrBO5E8sqY4_yZeD7 476Gi 476Gi 0Bi 100% 0 0 100% /Volumes/MobileBackups
/dev/disk2s1 30Gi 2.8Mi 30Gi 1% 88 973592 0% /Volumes/SD CARD

/dev/disk2 這就是 sd 卡

2. 卸載 disk2

$ diskutil unmountDisk /dev/disk2
Unmount of all volumes on disk2 was successful

3. 把下載回來的檔案安裝到 sd 卡

$ sudo dd bs=1m if=~/Downloads/2016-03-18-raspbian-jessie-lite.img of=/dev/rdisk2
Password:
1298+0 records in
1298+0 records out
1361051648 bytes transferred in 232.762720 secs (5847378 bytes/sec)

4. 把 sd 卡插入 raspberry pi 開機 預設的帳密如下

User: pi
Password: raspberry

5. 登入 raspberry pi 用 raspi-config 做一些設定 例如 “Expand Filesystem”, “Change User Password”

$ sudo raspi-config

在這個工具內可以順便指定 audio 輸出的介面
“Advanced Options” -> “Audio” -> “Force 3.5mm (‘headphone’) jack”
如果有 “Expend Filesystem” 會要求重新開機

6. 登入 raspberry pi 後測試一下聲音

$ speaker-test -t sine

如果沒辦法聽到聲音 編輯 alsa.conf

$ sudo vi /usr/share/alsa/alsa.conf

把 pcm.front cards.pcm.front 改成 pcm.front cards.pcm.default

7. 設定 wifi 輸入你指定的 ssid & password

$ sudo vi /etc/network/interfaces

allow-hotplug wlan0
auto wlan0

iface wlan0 inet dhcp
wpa-ssid “YOUR SSID”
wpa-psk “YOUR SSID PASSWORD”

重新啟動網路

$ sudo service networking reload

順利的話應該會看到下列的結果

wlan0 Link encap:Ethernet HWaddr 74:da:38:5a:ba:bf
inet addr:192.168.1.106 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::76da:38ff:fe6a:cacf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:318794 errors:0 dropped:20870 overruns:0 frame:0
TX packets:2351 errors:0 dropped:1 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:301272721 (287.3 MiB) TX bytes:577754 (564.2 KiB)

基本上設定到這裡就差不多準備切入正題了

8. 先對系統更新一下

$ sudo apt-get update

2019/10/21 補充說明:後來又更容易了用下面指令就可以安裝好

$ sudo apt-get install shairport-sync
$ sudo systemctl enable shairport-sync
$ sudo service shairport-sync start

詳細步驟請參考:9to5toys 也可以參考 :RPiPlay

9. 然後分別安裝下列套件

$ sudo apt-get install libao-dev libssl-dev git avahi-utils libwww-perl
$ sudo apt-get install libcrypt-openssl-rsa-perl libio-socket-inet6-perl libmodule-build-perl

10. 變成 root 再來準備安裝 shairport 我是懶得一直打 sudo 才變成 root

$ sudo -s

11. 裝之前, 先安裝 perl 的 Session Description Protocol 模組

$ cd /usr/local/src
$ git clone https://github.com/njh/perl-net-sdp.git
$ cd perl-net-sdp
$ perl Build.PL
$ ./Build
$ ./Build test
$ ./Build install

12. 正式安裝 shairport

$ cd /usr/local/src
$ git clone https://github.com/abrasive/shairport.git
$ cd shairport
$ make
$ make install
$ systemctl enable avahi-daemon

13. 設定 shairport 開機自動啟動

$ vi /etc/systemd/system/shairport.service
[Unit]
Description=Shairport AirTunes receiver
After=sound.target
Requires=avahi-daemon.service
After=avahi-daemon.service

[Service]
EnvironmentFile=/etc/conf.d/shairport
ExecStart=/usr/local/bin/shairport $SHAIRPORT_ARGS -w
Restart=always

[Install]
WantedBy=multi-user.target

14. 由於 /etc/conf.d/shairport 還沒建立所以要自行建立及設定參數

$ mkdir /etc/conf.d/
$ vi /etc/conf.d/shairport
# ShairportSync Daemon options
SHAIRPORT_ARGS=”-a YOUR AIRPLAY NAME”

15. 啟動 shairport 服務測試

$ service shairport start

16. 確定沒問題後就設定為開機啟動

$ systemctl enable shairport

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.