Raspberry Pi是基於ARM架構的卡片電腦,其小巧(信用卡大小),低功耗(手機充電器就可以給其供電),無噪音(無風扇)的特點吸引了我。我給它安裝了基於debian的rasbian linux系統。由於其低功耗和無噪音的特性,可以24小時不間斷運行。最近正好在海外出差,爲了提高蹩腳的英語聽力決定用我的Pi製作BBC網絡收音機。
思路:
Linux+Mplayer
Rasbian是基於Debian的Linux系統,所以大部分命令行軟件都可以使用。
先安裝Mplayer
pi@raspberrypi:~$ sudo apt-get install mplayer
給Raspberry Pi安裝聲音服務
sudo apt-get install alsa-utils
加載驅動
sudo modprobe snd_bcm2835
編譯DEMO
cd /opt/vc/src/hello_pi/ ./rebuild.sh
測試DEMO
cd hello_audio
3.5毫米輸出
./hello_audio.bin
用mplayer播放網絡流媒體(BBC)
pi@raspberrypi:~$ mplayer -loop 0 -cache 1024 -af volume=-10 mms://livewmstream-ws.bbc.co.uk.edgestreams.net/reflector:38972 < /dev/null > /dev/null 2>1&
其中volume值是調整音量
-cache 1024 是爲了緩存,如果網絡特別好的話這個就不用了。但是執行命令行後需要等待一段時間緩存後才能聽到收音機。
上面這個命令每次都輸入的話非常繁瑣,所以我做成了shell腳本,保存爲文件名bbc1
pi@raspberrypi:~$ cat bbc1 #!/bin/sh sudo kill -s 9 `ps -aux | grep mplayer | awk '{print $2}'` mplayer -loop 0 -cache 1024 -af volume=-10 mms://livewmstream-ws.bbc.co.uk.edgestreams.net/reflector:38972 < /dev/null > /dev/null 2>1&
這樣可以通過以下命令來執行
pi@raspberrypi:~$ sh ./bbc1
如果想要讓每次Pi啓動的時候自動播放BBC則可以修改/etc/rc.local文件
在exit 0上面一行
pi@raspberrypi:~$ sudo vi /etc/rc.local
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # Print the IP address _IP=$(hostname -I) || true if [ "$_IP" ]; then printf "My IP address is %s\n" "$_IP" fi sh /home/pi/bbc1 exit 0
保存後以後每次啓動Raspberry Pi,只要有網絡的話都可以自動播放BBC的。
如果對Raspberry Pi自帶的3.5mm音頻輸出口的音質不滿意的話可以從網上淘一個 USB DAC,音質提升很明顯。這個下期再討論。
下期預告:Raspberry Pi變成無損HiFi音樂播放器