My Raspberry Pi Music Box — Part 1
I turn my Raspberry Pi into my music box lately and I found it very useful and enjoyable to play with. Let my Raspberry Pi play a song from Spotify, YouTube URL or playlist, or even play an MP3/FLAC file directly from the storage server. In this article, I’ll share my setup with my Raspberry Pi Music Box and I hope you learn something from it.
Here’s the Part 1 and I’ll talk about spotifyd
. This tool allow me to play Spotify song from my Raspberry Pi.
Spotify Daemon Music Player
Note: You need Spotify Premium account to use spotifyd
spotifyd
will create a process that running in the background that will connect your device to the Spotify Server so you can play music using Spotify Connect. Here’s the required step that you need to do to enjoy spotifyd
.
- Download the binary from the releases page and the extract it using
tar command
. Raspbian already havetar
built-in package, so you can extract it directly without installing anything. Don’t forget to give an executable permission to the binary by executingchmod +x spotifyd
command. - Create a configuration file named
spotifyd.conf
in~/.config/spotifyd/
. Here’s the configuration that I use in my Raspberry pi, you just need to replace the placeholder with your credentials.
[global]
username = <SPOTIFY_USERNAME>
password = <SPOTIFY_PASSWORD>
device_name = <DEVICE_NAME>
bitrate = 320
backend = alsa
device = sysdefault
cache_path = /home/pi/cache/spotifyd
device_type = speaker
For the device
section, you can use your own hardware from executeaplay -L
in your terminal console. Pick one then remove :CARD
from the output. For example, I want to use sysdefault:CARD
so I put device = sysdefault
in the spotifyd.conf
and if I want to use plughw:CARD
I’ll use device = plughw
.
- From now on, you can play Spotify song from your Raspberry Pi device, but we want to make it daemonize. We’re using
supervisord
to make it happen. I’m not gonna tell you how to install and configuresupervisor
and run it everytime you start your box (systemd
stuff), but I’ll share you how to make thespotifyd
running daemonized. You can add these section into yoursupervisord.conf
to register the process.
[program:spotifyd]
command=/usr/bin/spotifyd --no-daemon --config-path /home/pi/.config/spotifyd/spotifyd.conf
directory=/home/pi/.config/spotifyd
process_name=%(program_name)s
priority=1
user=root
group=root
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/supervisor/spotifyd.log
$ supervisorctl status
spotifyd RUNNING pid 6373, uptime 0:02:36$ supervisorctl tail spotifyd
Connecting to AP "gae2-accesspoint-b-xmtb.ap.spotify.com:443"
Authenticated as "<SPOTIFY_USERNAME>" !
Country: "ID"
Using alsa sink
<spotify:track:0PLMmmX34RabWBYlW3m4qR>
<Belly Dancer> loaded
That’s it. Enjoy your Spotify music box :D Next time in the Part 2, I’ll you share how to play YouTube videos (music video to be more specific) from your Raspberry Pi without generating a Google Apps API for YouTube. The method that I’ll be sharing next time support playing live videos and playlist.