Raspberrypi Spotify Connect

Stream music from Spotify to Raspberrypi using Spotify Connect.

Spotify Connect

Setup Spotify Connect

A lot of time, you will prefer convenience over audio quality. Spotify provides that. On the date I am writing this blog, Spotify only streams in 320 kbps. But they provide a very nice pluggin to stream music to other devices which works very similar to UPnP/Chromecast. The music stream doesnt actually passes through your phone, but your phone acts as a controller and music stream is directly fetched from Spotify server. Whereas, Airplay is like bluetooth, in which your phone needs to be up in order for stream to work.

To setup Spotify Connect we will use a nice library spotifyd. You can download the package from this page. Below code will download the package for armv6 architecture.

wget https://github.com/Spotifyd/spotifyd/releases/download/v0.3.5/spotifyd-linux-armv6-slim.tar.gz

Below code will unzip the file. Post that you can see the binary which you can run. Move the binary to bin folder.

tar xzf spotifyd-linux-armv6-slim.tar.gz
sudo mv spotifyd /usr/bin/

Spotifyd uses config file to setup feature. To create the configuration file, run below code and paste the following configuration.

sudo nano /etc/spotifyd.conf

# If set to true, 'spotifyd' tries to bind to dbus (default is the session bus)
# and expose MPRIS controls. When running headless, without the session bus,
# you should set this to false, to avoid errors. If you still want to use MPRIS,
# have a look at the 'dbus_type' option.
use_mpris = false

# The audio backend used to play music. To get
# a list of possible backends, run 'spotifyd --help'.
backend = "alsa"

# The alsa audio device to stream audio. To get a
# list of valid devices, run 'aplay -L',
device = "hw"

# The PCM sample format to use. Possible values 
# are F32, S32, S24, S24_3, S16. 
audio_format = "S24"

# The alsa control device. By default this is the same
# name as the 'device' field.
control = "hw"

# The alsa mixer used by 'spotifyd'.
mixer = "Digital"

volume_controller = "alsa_linear"

# The name that gets displayed under the connect tab on
# official clients. Spaces are not allowed!
device_name = "Living-Room-Speakers"

# The audio bitrate. 96, 160 or 320 kbit/s
bitrate = 320

# If set to true, audio data does NOT get cached.
no_audio_cache = true

# Volume on startup between 0 and 100
# NOTE: This variable's type will change in v0.4, to a number (instead of string)
initial_volume = "40"

# If set to true, enables volume normalisation between songs.
volume_normalisation = false

# After the music playback has ended, start playing similar songs based on the previous tracks.
autoplay = true

To automatically run the binary at boot in backgroud we can create a systemd service file. Execute below code to achieve that.

sudo nano /lib/systemd/system/spotifyd.service

Paste below configuration into the servce file.

Description=A spotify playing daemon

ExecStart=/usr/bin/spotifyd --no-daemon


Now we need to start and enable our systemd service. To do that run following code.

sudo systemctl start spotifyd.service 
sudo systemctl enable spotifyd.service

Once is it started, reboot your pi and check the status of spotifyd.service by below command. It should show active(running).

systemctl status spotifyd.service

Open your spotify and you should see your raspberrypi under device. Connect to it and start playing!

Vivek Parashar