This tutorial will guide you through all the steps necessary to stream Spotifyto your Pi, using Mopidy,Mopidy-Spotify, and theIris web interface. The endresult will be a streaming Spotify box that you can control remotely fromanother computer in your house or from your phone (Android or iOS) or tablet.
For controlling the Mopidy from frontend you can install some extensions. Also, it can be easily run on the Raspberry P either using Raspbian or Arch Linux to create an affordable Jukebox. Moreover, has the capability to play music from your local disk and from cloud services like Spotify, SoundCloud, and Google Play Music via an extension on.
Note that as of Feburary 2020, only Mopidy-MPD (automatically installed) and Mopidy-Spotify (RPMFusion-nonfree) are packaged. Extension for playing music from the Spotify music streaming service. Around 2 days ago I discovered Mopidy, an MPD 'clone' that adds support for Spotify, Soundcloud, and other services. The first thing I thought was: 'Hey! I can use ncmpcpp again!'.
We've put together a neat little one-line-installer to make this whole processreally easy, but we'll also explain what it does later, because you wouldn'tbe learning otherwise, huh?
It will work either with the built-in 3.5mm stereo jack on your Pi model B+, 2,or 3, or with our audio boards -pHAT BEAT,pHAT DAC, orSpeaker pHAT. It'salso perfect for thePirate Radio Kit,turning it into a neat little streaming audio box.
Note that you'll need a Spotify Premiumaccount for this, although you can use this setup to play local music files,like MP3s, as well.
We recommend using the very latest version of Raspbian Lite, and you'll needan internet connection to install the Mopidy packages later (we'll explain howto do that in Raspbian Lite below). We'd recommend using either the NOOBSinstaller, which you can find here(ensure that you select Raspbian Stretch Lite when installing), ordownloading Stretch Lite from hereand then using the Etcher tool to burn the image to yourSD card.
Currently, our spotipy installer is best supported by Stretch Lite, and maynot work properly in the full version of Raspbian. Because Stretch Lite is..liter (sic) it should also run a bit more snappily than the full Raspbian.
Using the Pi's built-in 3.5mm stereo jack
If you're using the built-in audio on your Pi model B+, 2, or 3, then there's noother preparation needed.
Using pHAT DAC
If you're using our pHAT DAC board, then you need to solder the 2x20 pin femaleheader (that comes with it) to your pHAT DAC (and also solder a 2x20 pin maleheader to your Pi Zero or Pi Zero W, if you're using one). We have a guide onhow to solder headers to pHATs here.
Pop the pHAT DAC onto your Pi's GPIO pins.
Using pHAT BEAT or the Pirate Radio Kit
Filezilla register. If you're using the Pirate Radio Kit and haven't already built it, then followour guide hereto learn how to put it all together.
If you're just using pHAT BEAT, then make sure that you've soldered the 2x20 pinfemale header to it. Again, you can follow ourguide to solderingpHATs. Connect a couple of speakers (or just one) to your pHAT BEAT's push fitspeaker terminals.
Pop the pHAT BEAT onto your Pi's GPIO pins, if you haven't already.
Using Speaker pHAT
If you're using Speaker pHAT, then follow our guide to soldering ithere.
Pop the Speaker pHAT onto your Pi's GPIO pins.
Running the installer
Just to reiterate, you'll need an internet connection to run this installer.Pop the SD card into your Pi, make sure that the audio board that you're usingis attached to your Pi (on its GPIO pins) if you're using one, and then plug inthe power and boot up your Pi. Because you're using Stretch Lite, your Pi willboot straight to a terminal, but don't be scared!
You'll need to connect to Wi-Fi. To do this, you need to edit a file calledwpa_supplicant.conf
. Type the following:
Use the arrow keys to move to the bottom of the file, and add the following,replacing the parts that say 'YOURSSID' and 'YOURPASSWORD' with theSSID and password for the Wi-Fi network to which you'd like to connect.
Once you've made those changes, press control
and x
, then y
, and thenenter
to save and close the file.
Now, type sudo reboot
and press enter
to reboot. Your Pi should now beconnected to Wi-Fi.
To run the installer, type the following:
You'll need you Spotify username and password handy for this part. The installerwill ask you to enter your username and password and will use this to configurethe Mopidy-Spotify plugin. Note that your username and password will be storedin plain text so it's a good idea, for safety's sake, to have a unique password(i.e. to make sure it isn't the same as any other passwords that you use), andthat you don't have any open, insecure ports on your local area network.
You'll also need a client_id
and client_secret
which you can obtain by following this link, clicking on the green 'Authenticate Mopidy with Spotify' button and logging into your Spotify account.
The installer will prompt you several times, and you should type y
for all ofthese, with the exception of the device selection. When it prompts you to selecta device, then type the number of the device that you'd like to install. Notethat if you're using the built-in audio through the 3.5mm jack then you shouldselect option 0 here.
The installer should also ask if you'd like to reboot once the installation iscomplete. If it doesn't, then restart by typing sudo reboot
again.
What the installer does
If you're not concerned with what the installer does, then stop reading thissection now! If you are, then read on! It's good to know exactly what it does incase you want to pull down our installer script and modify it for your ownpurposes (which you're very welcome to do!)
Our installer installs Mopidy, and a bunch of plugins to add functionality toit. Mopidy is a network music server, that allows you to stream music over yourlocal network or, in this case, from internet music sources like Spotify.
Here, we're using Spotify as our music source, so we're installing theMopidy-Spotify plugin, which allows you to play songs, albums, playlists, andyour own saved music on Spotify.
The Mopidy-ALSAMixer plugin allows you to control the system volume levelstraight from Mopidy, and will also allow us to hook it into our physicalbuttons on pHAT BEAT (if you're using it).
The physical buttons on pHAT-BEAT are hooked up straight to the GPIO on your Pi.There's a handy Mopidy plugin called Mopidy-TTSGPIO that lets you assignfunctions to buttons connected to GPIO pins; functions like play/pause, forward,back, volume up and down, and shutdown. Fortunately, the buttons on pHAT BEATand the front panel on Pirate Radio are labelled up with those exact functions!
As well as allowing the use of GPIO-linked buttons, this plugin also allowstext-to-speech to announce track and playlist names, and to confirm buttonpresses, amongst other things, although we don't enable this as part of theinstaller script.
The Iris web front-end provides a really slick-looking interface to controlyour Pi running Mopidy. It caters specifically for Spotify, so it's set upwith menu options for playlists and latest releases, etc. If you're not usingSpotify and instead are using local music, then it might be best to look intoone of the other Mopidy web front-ends.
Our installer will also install and configure the software for your chosenmusic output device, be it pHAT DAC, pHAT BEAT, Speaker pHAT, or just the Pi'sbuilt-in audio.
As we mentioned previously, the installer adds your Spotify credentials to theMopidy configuration file (/etc/mopidy/mopidy.conf
). It also enables theappropriate plugins mentioned earlier, sets up the correct functions for thebuttons on pHAT BEAT (if you're using it), and makes the Pi's IP addressvisible on your local area network, so that you can control it remotely on yourPC/Mac/phone/tablet via the web front-end.
Lastly, it runs Mopidy as a system service, meaning that it will fire everythingup automatically when your Pi boots.
Booting for the first time, and using Mopidy
As we said, now that everything is installed and set up, Mopidy should fireitself up when your Pi boots. If your Pi rebooted after the installer ran, thenMopidy may well be running already! It takes a couple of minutes to fireeverything up, and maybe a little longer on a Pi Zero or Pi Zero W, so onceyou've plugged the power into your Pi, or rebooted it, then wait for a fewminutes before following the steps below.
You'll need to know your Pi's IP address to access the web front-end remotely.If you have a display and keyboard attached to your Pi (we'll assume you do,since you ran the installer), then type ifconfig
in the terminal to find yourIP address. If this is the only Raspberry Pi on your network, and you haven'tchanged the hostname of your Pi, then you will be able to useraspberrypi.local
instead of the IP address.
Open a browser window on your PC/Mac/phone/tablet and enter the following URL:
where 192.168.0.2
is the IP address of your Pi, or if you're going to useraspberrypi.local
then enter:
Your browser should have loaded up the Iris web interface. It's fairly self-explanatory to navigate, but one important thing you'll have to do is to goto the settings page and log in to Spotify, to authorise it, if it's the firsttime you're using it.
Click 'log in', and it should pop up a new window to authorise Spotify. If itdoesn't, then you might need to switch off any pop-up blockers that might beactive. Now that you've authorised Spotify, you should have access to yourplaylists, and saved albums and artists, featured Spotify playlists, and more.
Because Iris is responsive, it looks really good on mobile as well, resizing andchanging context to work on smaller displays. And because it's just a regularweb front-end it will work equally well in iOS or Android.
With the Mopidy-TTSGPIO installed and active, you should be able to use thebuttons along the side of pHAT BEAT to control your music. Try them!
Taking it further
Mopidy Spotify Free
Why not try some of the Mopidy webfront-ends, or some of theother plugins to use serviceslike Google Play Music, SoundCloud, TuneIn radio, and YouTube. Or try to add anOLED display to showtrack and artist information? You could try to make your Pi music player Twitteror Slack controlled, turning it into a collaborative music player!
Nice, I play spotify premium streaming audio using mopidy on debian now 🙂
I approximately did this:
I got debian debian-6.0.7-i386-netinst
[I have to add though that this configuration can almost be applied to ubuntu as well,
note that I use root access, so you may apply 'sudo' where you like it, I can't be bothered, as I will explain later, so read on]
Next I dumped that on a 4Gb USB apparatus that I had laying abouts, with the tool: unetbootin-windows-583
[Linux users can probably dump that using a default available tool that comes with their favourable installation, but do contemplate the fact that it is only a lengthy lot of 0 and 1 combinations. With this I mean the dumping of the iso contents as well as your favourable software installation you use on your computing device]
Installed that debian on the computing device I had laying arounds.
Updated with
probably rebooted to load the new kernel image, but also tweaked the network, I won't go into networking here.
I had some problems with iwlwifi from intel that you can solve with:
specifically:
Addding the line 'deb http://ftp.nl.debian.org/debian squeeze main contrib non-free' to /etc/apt/sources.list
I'm sure you can find out a location other than ftp.nl… in your neighbourhood.
and then:
and finally checking if the settings frotted into /etc/network/interfaces worked with:
Mopidy Local
With networking running and functioning it was time to add the mopidy repository to apt using commands freely available from the mopidy main page, being:
start mopidy [wait a few seconds, ctrl-c to break it again]:
{ctrl-c}
[YES I DO THIs as root, sue me, just read on…]
config directory should now exist in /root/.config/mopidy
put in place a decent config, mine is: [/root/.config/mopidy/settings.py]:
MPD_SERVER_HOSTNAME = u'-your-local-ip-here-‘
SPOTIFY_USERNAME = u'spot-user'
SPOTIFY_PASSWORD = u'spot-pwd'
BACKENDS = (
u'mopidy.backends.local.LocalBackend',
u'mopidy.backends.spotify.SpotifyBackend',
)
FRONTENDS = (
u'mopidy.frontends.mpd.MpdFrontend',
)
HTTP_SERVER_HOSTNAME = u'-your-local-ip-here-‘
LOCAL_MUSIC_PATH = u'/data/audio/remote'
LOCAL_PLAYLIST_PATH = u'/data/playlists'
LOCAL_TAG_CACHE_FILE = u'/data/tagcache/tag_cache_mop'
MIXER = u'alsamixer'
MIXER_TRACK = u'Speaker'
SPOTIFY_BITRATE = 320
SPOTIFY_CACHE_PATH = u'/data/spotifycache'
As sound didn't work with using these MIXER and MIXER_TRACK settings I added an asound.conf to /etc that didn't exist before. This sets the alsa sound card to use as a default. I will explain why I do this, namely, I do this because: this machine has a default built in sound card that I find terribly horrible, so I added a second card, using USB technology.
Alsa says that the terrible card is card 0 (zero), the splendid usb module on the other hand is card 1 (one) in alsa terms.
See, card 1 is actually card 2, or in human terms, the second card or board, represented by the arabic sign for 1. This is also called a number. Sort of like in index. It being the second card while numbering it 1 (one) sounds pretty stupid, but some people think 0 is something too and start counting from something, rather than from nothing, which is philosophically correct, just mind numbing. It was for me. One programmer starts counting from 1, and another from 0. Or starts counting from 0, but labels it some text that says 1. Anyway, I'm more relaxed about it now.
Actually. now that I think about it again, it's so stupid. What if you have a system with no sound card (for some reason) and you have alsa installed (for some reason). You can't check if there are 0 sound cards, coz that would mean there would be 1 sound card. As a programmer you would have to fall back to check for something like > -1 which is bullshit. Make the alsa system itself card 0 so that you can check card > 0, which would be more sound err. workable.
Anyway, default sound card is 1 by setting asound.conf:
See asound.conf in /etc:
That at least worked in getting some output.
I did start alsamixer and set its card numero uno (1) as I wanted, quit the program with [ESC] and performed the command
This stores the settings, so that you can restore them at a later time. To restore the settings for this card, I perform an alsactl command on startup in /etc/rc.local [always make sure rc.local ends with the exit 0 line]
As you can see in the settings.py config for mopidy I have a map [directory, folder, location] called /data/audio/remote
It is secretly not really a local location for files, no, it is smb mounted.
I do that with the following command in /etc/rc.local:
Where user is the smb user to use connecting with pwd as its corresponding password.
My guess is that you need some smb tools installed.
Yes, I start mopidy as root, so it runs as root, and it ever will.
I use only the above mentioned software on this installation of debian [only] and explicitly nothing more [exept for another instance of mpd]. It is behind a firewall.
I can imagine highly educated persons using this software remotely, turning music on in their dwelling remotely, say, at work, when you are far away, or from other random locations, e.g. from outer space. However, in this particular case, I won't. I believe this strengthens my case for running things as root.
To apply all this evil on startup I added this line to /etc/rc.local
Configure and add your server to your mpd controlling application [client] and you can now search and play spotify.
Do read about what the guys from mopidy say about the clients, the clients aren't all that transparent or usable in this configuration.
I use MPaD on my apple device and MPDroid on my Android device, it works.
MPaD is slow at searching the first 3 characters, this kind of sucks.
A more usable option is to use a regular spotify application to create nice playlists, and select to play those playlists using MPaD.
I made bunch of playlists for certain albums of favourable artists, and easily select to play them using MPaD.
I tried this with my Android device as well, and DroidMPD doesn't work so well.
MPDroid works quite some better, especially with searching.
Good luck, if you have any questions read
and about mpd at
Thanks to hackaday.com, that I read regularly, to cover an article on the use of mopidy, so I could enable this brilliant software in combination with Spotify streaming audio.
Mopidy team, thanks a bunch.
P.S. What I thought was that the mpd tag_cache and the tag_cache of mopidy would work together. This is not entirely true. Possibly mopidy means to be compatible with it, but aparantly I had mopidy crashing on it, when using the mpd cache for the same purpose (at the same time, as I run mpd as well on a second IP on the same interface.)