How to create your own wifi-enabled (Sonos-like) music player for Soundcloud, Youtube, Spotify and your music files using a Raspberry Pi 4 with Mopidy
- raspberry pi 4
- sd card
- PC
You will need a linux version that is compatible with Mopidy and its various extensions. In this tutorial, we will be using 32-bit version of Ubuntu 19.10.1 (Spotify will not work with an arm64 architecture). If you do not have this OS already running on your pi, install it as follows:
- on your PC, wipe your sd card: https://www.windowscentral.com/how-clean-and-format-storage-drive-using-diskpart-windows-10
- download the armhf raspberry pi 4-compatible Ubuntu 19.10.1 image: http://cdimage.ubuntu.com/releases/eoan/release/ubuntu-19.10.1-preinstalled-server-armhf+raspi3.img.xz
- extract/uncompress the downloaded file using something like 7zip
- download and install win32 disk imager and write the downloaded img file to your sd card (follow the instructions listed here for more details on this: https://ubuntu.com/tutorials/create-an-ubuntu-image-for-a-raspberry-pi-on-windows#2-on-your-windows-machine)
- insert your sd card into your rpi4 and turn it on
- on first boot, your username is ubuntu, as is your password (https://wiki.ubuntu.com/ARM/RaspberryPi#First_boot_.28Username.2FPassword.29). you will then be prompted to change your password
- install mopidy running the lines of code indicated here: https://docs.mopidy.com/en/latest/installation/debian/
- in
~/.config/mopidy/mopidy.conf
, change the hostname value to::
- You can now view the app root at ubuntuUsername@ipAddressOfYourRaspberryPi:6680 on any browser connected to your local wifi network
- So that you can install the musicbox gui that will allow you to control Mopidy, install pip:
sudo apt install python3-pip
- install Spotify (https://mopidy.com/ext/spotify/):
sudo apt install mopidy-spotify
- follow the instructions at the bottom of the page, which tell you to paste the authenticated code at the bottom of your conf file (
~/.config/mopidy/mopidy.conf
), along with your username and password as indicated here https://github.com/mopidy/mopidy-spotify#configuration)
- follow the instructions at the bottom of the page, which tell you to paste the authenticated code at the bottom of your conf file (
- install Soundcloud (https://mopidy.com/ext/soundcloud/):
sudo apt install mopidy-soundcloud
- follow the instructions at the bottom of the page (by pasting the authenticated code at the bottom of your conf file, like you did with Spotify)
- install Youtube (https://mopidy.com/ext/youtube/):
sudo python3 -m pip install Mopidy-YouTube
- if you want mopidy to run automatically on startup, then paste the Soundcloud and Spotify code you added to
~/.config/mopidy/mopidy.conf
into/etc/mopidy/mopidy.conf
(this is the config file for mopidyctl, the systemd version of mopidy which will start on system startup). - You will also need to add the hostname value that you changed to
/etc/mopidy/mopidy.conf
as follows:
[http]
hostname = ::
- Then, to enable mopidy to run automatically on startup (https://docs.mopidy.com/en/latest/running/service/#service-management-with-systemd):
sudo systemctl enable mopidy
-
Finally, you need to make sure your sound output is properly set up. Connect your raspberry pi via USB to a soundcard of your choice, amplifier and speakers. As mentioned here (https://docs.mopidy.com/en/latest/audiosinks/), run the following and if you hear an audible sound, then you should be good to go:
gst-launch-1.0 audiotestsrc ! audioresample ! autoaudiosink
. If you don't hear anything, then you need proceed with the following:- run
sudo apt install alsa-base
- set the following
options snd-usb-audio index=0
in/etc/modprobe.d/alsa-base.conf
- restart your raspberry pi (
sudo shutdown -h -r now
) - after startup, things should be running fine (try running
gst-launch-1.0 audiotestsrc ! audioresample ! autoaudiosink
again play or any song on your mopidy client)
- run
-
PS. You might want to increase the Spotify bitrate for better sound quality, so add the
bitrate = 320
arg to/etc/mopidy/mopidy.conf
under[spotify]