Turn Your Raspberry Pi Into a Spotify Server
-
Upload
ariel-macz -
Category
Documents
-
view
12 -
download
6
description
Transcript of Turn Your Raspberry Pi Into a Spotify Server
-
Turn your Raspberry Pi into a Spotify server
In this tutorial we will install Mopidy on our Raspberry Pi. This will allow us to use the Pi
as a music server where we can access all of our local music files, as well as playlists from
e.g. Spotify, and stream it directly to our TV or speaker system.
What is Mopidy?
Mopidy is a music server that can play music from a wide variety of sources, like your local
hard drive, radio streams, Spotify, and SoundCloud. The nice thing about Mopidy is that it
collects all of your music sources in one place. This makes it a perfect match for our
Raspberry Pi.
Another cool feature is that when you search your library, it combines results from all
music sources, and you can mix tracks from all sources in your play queue. That includes
your playlists from e.g. Spotify, SoundCloud, or other available sources.
To control your Mopidy music server, you can use a wide variety of web clients, the
Ubuntu Sound Menu, any device on the same network which can control UPnP
MediaRenderers, or any MPD client.
There are MPD clients available for many platforms, including Windows, OSX, Linux,
Android and iOS. We will look into some of these in the end of this article.
Using these clients, Mopidy allows you to control your music from any device. And if you
make your Pi accessible from outside the LAN, you can literally control your music from
anywhere in the world!
Installation
0. Add needed modules
As a first step we need to activate some modules to avoid potential problems during the
installation. First, run the following command to activate ipv6:
sudo modprobe ipv6
Now open /etc/modules and add this to make sure modules are loaded on reboot:
snd-bcm2835
bcm2708_wdog
ipv6
-
1. Add the archives GPG key
Run the following command to add Mopidys GPG key:
wget -q -O - http://apt.mopidy.com/mopidy.gpg | sudo apt-key add -
2. Modify your sources.list
Add the following to /etc/apt/sources.list, or if you have
the directory/etc/apt/sources.list.d/, add it to a file called mopidy.list in that
directory:
# Mopidy APT archive
deb http://apt.mopidy.com/ stable main contrib non-free
deb-src http://apt.mopidy.com/ stable main contrib non-free
3. Install Mopidy and all dependencies:
sudo apt-get update
sudo apt-get install mopidy
4. Create the Mopidy config file
Now that we have installed Mopidy, its time to make some modifications to its config file. Right now we dont have a config file, and we can either create on manually, or let Mopidy generate one for us that will contain all the default values. We are not stupid, so lets do the latter.
In order to have Mopidy create the config file for us, we simply have to start Mopidy. If it
cannot find a config file, it will create a new one at startup. The file will be created in
~/.config/mopidy/mopidy.conf.
Run Mopidy:
mopidy
and you should see something like this:
INFO Starting Mopidy 0.18.3
INFO Loading config from: builtin defaults, /etc/xdg/mopidy/mopidy.conf,
/home/pi/.config/mopidy/mopidy.conf, command line options
INFO Enabled extensions: mpd, http, stream
INFO Disabled extensions: local
WARNING Found local configuration errors, the extension has been
automatically disabled:
WARNING local/media_dir must be set.
WARNING Please fix the extension configuration errors or disable the
extensions to silence these messages.
INFO Starting Mopidy audio
-
INFO Starting Mopidy backends: StreamBackend
INFO Audio output set to "autoaudiosink"
INFO Audio mixer is using software mixing
INFO Starting Mopidy core
INFO Starting Mopidy frontends: HttpFrontend, MpdFrontend
INFO MPD server running at [::ffff:127.0.0.1]:6600
INFO HTTP server running at http://127.0.0.1:6680
Stop the server with Ctrl-C.
Now we have the configuration file in place. If you want to, you can take a look at it to see
how the default settings are. We will edit a few things later on.
6. Restart the system
Now we need to restart the system to make sure all the new modules and dependencies are
working correctly:
sudo reboot
A very important note for RaspBMC users. Due to a dependency version problem that is
caused when installing Mopidy, you might have to make some minor configurations for
XBMC to start properly (this depends on your systems version).
If you notice that XBMC is not starting but gets stuck in a loop, you need to make these
configurations. In short, what you have to do is the following:
Run the following command, and it should stop the loop and start up as normal:
sudo ln -sf /home/pi/.xbmc-current/xbmc-bin/lib/xbmc/system/libtag.so.1
/usr/lib/arm-linux-gnueabihf/libtag.so.1
Then you need to open /etc/rc.local and add the command to the end of the file to make
it persistent on reboot. Now it will not complain anymore when you restart your system.
You can read more about it in this post.
7. Audio output
Once you have restarted the system, we can start making some changes to our settings.
By default the audio will output to HDMI, and if you want to change that we need to
change this value in the amixer settings:
sudo amixer cset numid=3 1
The 1 in the end indicates that we want the audio to output to analog. Other options for this value are:
-
0=auto
1=analog
2=HDMI
You can test these settings by running:
aplay /usr/share/sounds/alsa/Front_Center.wav
and you should hear a voice saying front center.
8. Install Spotify extension
After all the basic settings for Mopidy are in place, we can install the Spotify extension.
This will allow Mopidy to connect to Spotify with your account and fetch all your playlists.
Run the following command to install the extension:
sudo apt-get install mopidy-spotify
9. Edit the Mopidy conf
Now its time to make some modifications to the config file. Lets open
the ~/.config/mopidy/mopidy.conf file again so we can add a few things.
Under the section [ audio], we need to change the output value:
[ audio]
...
output = alsasink
...
Remember to remove the # from the beginning of the line. You can leave all the other values as they are.
We also have to add a new section for the Spotify extension. Add the following anywhere
in the file:
[spotify]
username = yourusername
password = yourpassword
Next we will prepare Mopidy to accept client connections. Under the section [mpd], change
hostname to :: and set a password:
[mpd]
...
hostname = ::
password = yourpassword
...
-
The change in hostname makes it possible for us to connect to Mopidy from any IP
address, and not only from the Pi itself.
Leave the rest as default.
When you have done this we must restart Mopidy in order for it to load the new settings:
sudo pkill mopidy
mopidy
Now you can see that Mopidy is also loading the Spotify extension, and you should also see
that its loading all of your Spotify playlists.
Clients
Now that weve installed Mopidy, we can start playing around with different clients we want to use to connect to it. At this point you can connect any MPD client to Mopidy and
control your music. There are a few you can choose from, and the official Mopidy site lists
a few they recommend.
MPD Client
We have already prepared Mopidy to accept connections from MPD clients, so lets start with that.
If you have an Android phone, you can download MPDroid from here.
iPhone users can download MPoD from here.
In order to connect to your Mopidy server, make sure that your phone is connected to the
LAN and simply input the IP of the Pi along with the password you set for the mpd
connection in the Mopidy config.
Web client
You might want the option to connect to Mopidy through a web client, so lets set that up as well. This means anybody can access it from a browser, which is nice if you have friends
over and they want to search for a song from their phone or laptop.
There are a few HTTP clients for Mopidy that work really great. We will install Mopidy-
Webclient by woutervanwijk, but you can use anyone youd like.
Lets get the client:
sudo mkdir -p /opt/webclient
wget https://github.com/woutervanwijk/Mopidy-Webclient/archive/master.zip
unzip master.zip
-
sudo mv Mopidy-Webclient-master/flatclient/ /opt/webclient/
If you have problems getting the zip file due to an error like: ERROR: The certificate of `github.com' is not trusted.
Add the --no-check-certificate flag to the wget command
Now we need to edit the Mopidy config file:
vim .config/mopidy/mopidy.conf
Find the [http] section, and edit it to look like this:
[http]
enabled = true
hostname = ::
port = 6680
static_dir = /opt/webclient/flatclient
Restart Mopidy:
pkill mopidy
mopidy
Now you are able to connect to Mopidy through your browser. Simply enter the IP of you
Pi, and the port as specified in the config file (6680), e.g. http://192.168.0.93:6680/
This was a rather long tutorial, but hopefully it was worth it. Dont forget to share it if you liked it, and drop a comment if you have any question or need help with anything!