Turn Your Raspberry Pi Into a Home Music Server With Mopidy

Turn Your Raspberry Pi Into a Home Music Server With Mopidy #

BY PHIL KINGPUBLISHED APR 30, 2021

Learn how to turn your Raspberry Pi into a streaming music server that can even route Spotify to your living room with Mopidy

Modipy music web interface

Home audio systems from the likes of Sonos are all the rage, but they can be pretty pricey. With just a Raspberry Pi and some speakers, however, you can create your own DIY sound system to play locally stored music, Spotify streams, internet radio stations, podcasts, and more via a web interface.

This is all made possible by Mopidy, an extensible headless music server written in Python.

What You Will Need to Build a Music Server #

• A Raspberry Pi. Any model will work, although Mopidy will run significantly faster on later full-size models. If using a Pi Zero, you will need a DAC add-on board for wired audio output.

• A pair of powered speakers with 3.5mm male jack for audio input. Alternatively, you can output audio through your TV via HDMI.

• A wireless network to access the music server via a web interface.

1. Install Mopidy #

First, make sure you’re using the latest version of Raspberry Pi OS. Ideally, you should do a clean install on a microSD card using Raspberry Pi Imager.

To install the latest version of Mopidy and all its dependencies, enter the following commands in a Terminal window:

wget -q -O - https://apt.mopidy.com/mopidy.gpg | sudo apt-key add -sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/buster.listsudo apt updatesudo apt install mopidy

It may take a few minutes to download and unpack Mopidy.

Installing Mopidy music player

If you’re using the Raspberry Pi OS with desktop image, you will need to add the Mopidy user to the video group:

sudo adduser mopidy video

To make Mopidy run as a background service upon boot, enter:

sudo systemctl enable mopidy

2. Configure the Mopidy Settings #

To configure Mopidy’s settings so you can access it from a web browser on the Raspberry Pi or another device, you’ll need to edit the mopidy.conf file:

sudo nano /etc/mopidy/mopidy.conf

At the bottom of the file, add the following lines:

[http]hostname = 0.0.0.0

If you’re not using audio output via HDMI, also add these lines (omitting the space in [ audio]) to use the 3.5mm AV jack:

[ audio]output = alsasink device=hw:1,0

Press Ctrl + X, then Y to save the file.

Note that the device=hw:1,0 setting tells Mopidy to use sound card 1, device 0, which is the 3.5mm AV jack. If using a DAC add-on board instead, you’ll need to set it to a different value – for a list of audio hardware devices, enter this in a Terminal:

aplay -l

Start the Mopidy service running with:

sudo systemctl start mopidy

3. Access the Mopidy Web Interface #

Now try accessing the service from your Raspberry Pi or another device on the same network. In a web browser, enter the following address: http://raspberrypi.local:6680

You should see a web page with a message about Mopidy.

Mopidy web interface without UI

To access it properly, you’ll need to install a web client as a Mopidy extension. There are several available, but one of the most sophisticated is Iris. Install it with:

sudo python3 -m pip install Mopidy-Iris

You also need to run the following command (on one line) to give the mopidy user sudo permission to run the Iris installer:

sudo sh -c 'echo "mopidy ALL=NOPASSWD: /usr/local/lib/python3.7/dist-packages/mopidy_iris/system.sh" >> /etc/sudoers'

Restart the Mopidy service for the change to take effect:

sudo systemctl restart mopidy

Now try accessing the web interface, by visiting the following address:

http://raspberrypi.local:6680/iris/

You’ll see a web page welcoming you to Iris. Click Save and you’ll see the user interface.

Mopidy Iris web UI no music

4. Add Music to Mopidy #

OK, so now you need to add some music files to the Music folder in the /home/pi/ directory. Mopidy supports a wide range of audio codecs, including MP3 and FLAC. So transfer some music files any way you like, such as with a USB memory stick.

Next, you need to install the Mopidy-Local extension to read local files. In a Terminal window, enter:

sudo python3 -m pip install Mopidy-Local

Now reopen the Mopidy settings file with:

sudo nano /etc/mopidy/mopidy.conf

Then add the following lines to set it to look for music files in the Music folder:

[local]media_dir = /home/pi/Music

Restart the Mopidy service with:

sudo systemctl restart mopidy

Then do a scan to find your music files:

sudo mopidyctl local scan

You can also do this from the Iris interface, by going to Settings, scrolling down, and clicking the Start local scan button.

Note: It may take a little while for the tracks to appear in the interface. You may also need to click the Refresh option on the main screen.

Your tracks should now appear in the Iris interface and you can double-click on one to play it. Note that even when using a remote device to access the web interface, the audio will play from the Raspberry Pi.

Mopidy playing a track

Another aspect to note is that only album art embedded in the music files will show up. Even then, it’s a little hit and miss.

RELATED: Musical Instruments You Can Build With A Raspberry Pi

5. Stream From Spotify #

Mopidy also supports streaming services such as Spotify, YouTube Music, and SoundCloud. Here we’ll show you how to get Spotify working, although note that you’ll need a Premium account.

First, you need to install the Mopidy-Spotify extension. Open a Terminal window and enter:

sudo python3 -m pip install Mopidy-Spotify

You need to add your Spotify Premium username and password to your Mopidy configuration file and also visit the Mopidy website to authorize this extension against your Spotify account. This will give you a client_id and client_secret to use.

Reopen the mopidy.conf settings file with:

sudo nano /etc/mopidy/mopidy.conf

Add the following lines, replacing the values with your Spotify username, password, and the client_id and client_secret you obtained from mopidy.com:

[spotify]username = <your Spotify username>password = <your Spotify password>client_id = <your client_id>client_secret = <your client_secret>
Mopidy configuration for Spotify

As before, press Ctrl + X, then Y to save and exit.

Restart the Mopidy service with:

sudo systemctl restart mopidy

Now refresh your Iris web interface at raspberrypi.local:6680/iris/ and you should have full access to Spotify.

6. Add Internet Radio to Mopidy #

You can also listen to your favorite radio stations via TuneIn. To install the extension, enter:

sudo python3 -m pip install Mopidy-TuneInsudo systemctl restart mopidy

In the Iris web interface, select the Browse option and then TuneIn to access options such as Local RadioMusic (for a selection of genres), and Podcasts.

Mopidy TuneIn radio stations

For a more comprehensive selection of podcasts, you can install the Mopidy-Podcast extension with:

sudo python3 -m pip install Mopidy-Podcastsudo systemctl restart mopidy

This lets you browse podcasts distributed as RSS feeds and play individual episodes in a variety of audio formats. You can also add the optional Mopidy-Podcast-iTunes extension for it, which lets you search and browse podcasts from the Apple iTunes Store.

READ MORE: The Best Raspberry Pi Projects For Beginners

Creating Your Own Custom Music Player #

As well as experimenting with the other Mopidy extensions available (mopidy.com/ext), you could even add a mini LCD screen to make a portable player. One of the easiest ways to do so is with one of the Pirate Audio add-ons by Pimoroni, which have the bonus of a DAC (digital to analogue converter) for enhanced audio output quality.

However, you opt to use Mopidy, we’re sure you’ll have a great listening experience with your customisable Raspberry Pi music player.

Powered by BetterDocs