Home » Build a music streaming server with Mopidy and a Raspberry Pi

Build a music streaming server with Mopidy and a Raspberry Pi

by shedboy71

Building a music streaming server with Mopidy allows you to stream music from your Raspberry Pi to various devices over the network.

This tutorial will guide you through setting up Mopidy, configuring it, and adding extensions for enhanced functionality.

What is Mopidy?

Mopidy is a Python-based music server that can play music from local files, Spotify, YouTube, and other sources.

Here is a summary from the https://mopidy.com/ website

Mopidy plays music from local disk, Spotify, SoundCloud, TuneIn, and more. You can edit the playlist from any phone, tablet, or computer using a variety of MPD and web clients.

Vanilla Mopidy only plays music from files and radio streams. Through extensions, Mopidy can play music from cloud services like Spotify, SoundCloud, and TuneIn.

With Mopidy’s extension support, you can easily add backends for new music sources.

Mopidy is a Python application that runs in a terminal or in the background on Linux computers or Macs that have network connectivity and audio output.

Out of the box, Mopidy is an HTTP server. If you install the Mopidy-MPD extension, it becomes an MPD server too. Many additional frontends for controlling Mopidy are available as extensions.

Mopidy’s extension support and Python, JSON-RPC, and JavaScript APIs make Mopidy a perfect base for your projects.

 

Requirements

  1. A Raspberry Pi (any model; newer models recommended for better performance. I used my usual Raspberry Pi 4).
  2. MicroSD card (8GB or larger).
  3. Raspberry Pi OS (Lite or Desktop version. I used the desktop one as this was already setup on a few SD cards that I have).
  4. External storage (this is optional, but it is recommended for large music libraries. There are several solutions that can use external SSDs or M2 drives, obviously this ups the cost).
  5. Speakers or audio output device (for local playback).
  6. Network connection (Ethernet or Wi-Fi).

Step 1: Set Up the Raspberry Pi

Install Raspberry Pi OS

  1. Download and install Raspberry Pi Imager.
  2. Flash Raspberry Pi OS (Lite or Desktop) onto the microSD card.
  3. Enable SSH:
    • Create a file named ssh (no extension) in the boot partition of the microSD card.
  4. Insert the microSD card into the Raspberry Pi and power it on.

Access the Raspberry Pi

  1. Find the Pi’s IP address using your router or a network scanner.
  2. SSH into the Raspberry Pi:
    ssh pi@<IP_ADDRESS>
    

    Default username: pi, password: raspberry.

  3. Update and upgrade the system:
    sudo apt update && sudo apt upgrade -y
    

Step 2: Install Mopidy

  1. Install prerequisites:
    sudo apt install python3-pip python3-gi gstreamer1.0-plugins-{base,good,ugly,bad} -y
    
  2. Install Mopidy:
    sudo apt install mopidy -y
    
  3. Verify installation:
    mopidy --version
    

Step 3: Configure Mopidy

Set Up Configuration File

The configuration file is located at ~/.config/mopidy/mopidy.conf.

  1. Create or edit the configuration file:
    nano ~/.config/mopidy/mopidy.conf
    
  2. Add basic settings:
    [core]
    cache_dir = ~/.cache/mopidy
    config_dir = ~/.config/mopidy
    data_dir = ~/.local/share/mopidy
    
    (audio) - change to []
    output = alsasink
    
    [mpd]
    enabled = true
    hostname = 0.0.0.0
    port = 6600
    
    [http]
    enabled = true
    hostname = 0.0.0.0
    port = 6680
    zeroconf = Mopidy HTTP server on $hostname
    
  3. Save and exit the file.

Step 4: Add Music Sources

Local Music Files

  1. Create a directory for music:
    mkdir ~/Music
    
  2. Add your music files to this directory (e.g., via SCP or USB).
  3. Enable the local backend in the configuration:
    [local]
    enabled = true
    media_dir = ~/Music
    
  4. Scan the music library:
    mopidy local scan
    

Spotify (Optional)

  1. Install the Mopidy-Spotify extension:
    sudo apt install mopidy-spotify -y
    
  2. Obtain Spotify credentials:
  3. Add Spotify settings to the configuration:
    [spotify]
    enabled = true
    username = <your-spotify-username>
    password = <your-spotify-password>
    client_id = <your-client-id>
    client_secret = <your-client-secret>
    

YouTube (Optional)

  1. Install the Mopidy-YouTube extension:
    pip install Mopidy-YouTube
    
  2. Add YouTube settings to the configuration:
    [youtube]
    enabled = true
    api_key = <your-youtube-api-key>
    

    Obtain an API key from the Google Cloud Console.

Step 5: Run Mopidy

  1. Start Mopidy manually to test:
    mopidy
    
  2. Access the web interface:
    • Open a browser and navigate to http://<Raspberry_Pi_IP>:6680.
  3. If everything works, run Mopidy as a service:
    sudo systemctl enable mopidy
    sudo systemctl start mopidy
    

Step 6: Install Clients

Mopidy supports multiple clients for controlling playback:

Web Client

  • Use the built-in web client available at http://<Raspberry_Pi_IP>:6680.

MPD Clients

  1. Install an MPD client like ncmpcpp on Linux:
    sudo apt install ncmpcpp -y
    
  2. Configure it to connect to the Mopidy MPD server at port 6600.

Mobile App Options

  • iOS: Install apps like M.A.L.P. or [MPD Client].
  • Android: Use apps like MPDroid.

Step 7: Optimize and Customize

Enable Auto-start

  1. Enable Mopidy as a service:
    sudo systemctl enable mopidy
    
  2. Start the service:
    sudo systemctl start mopidy
    

Add More Extensions

Explore more Mopidy extensions to add functionality, such as:

  • Mopidy-SoundCloud: Stream SoundCloud music.
  • Mopidy-TuneIn: Access TuneIn radio stations.

Install extensions using pip:

pip install Mopidy-SoundCloud

Improve Performance

  1. Increase cache size for better performance:
    [local]
    scan_flush_threshold = 100
    
  2. Use a wired connection for faster streaming.

Step 8: Troubleshooting

  1. Check Logs:
    journalctl -u mopidy
    
  2. Test Audio Output:
    speaker-test -t sine -f 440 -c 2
    
  3. Verify Configuration: Mopidy shows errors when misconfigured. Verify settings in mopidy.conf.

Conclusion

By following these steps, you’ve set up a music streaming server with Mopidy on your Raspberry Pi.

You can stream music from local files, Spotify, YouTube, and more, all accessible from multiple devices on your network.

You can extend its capabilities further by exploring additional plugins and third-party clients!

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More