Skip to content

Instantly share code, notes, and snippets.

@maphouse
Last active May 11, 2020 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maphouse/5468da4b6a086ac696e83aae31bec7e7 to your computer and use it in GitHub Desktop.
Save maphouse/5468da4b6a086ac696e83aae31bec7e7 to your computer and use it in GitHub Desktop.

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

material

  • raspberry pi 4
  • sd card
  • PC

installing Ubuntu 19.10.1

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:

installing Mopidy

  • 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

installing the necessary extentions

  • 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)
  • 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

Setting up Mopidy to run automatically on your machine

  • 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 = ::

Configuring your sound output

  • 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)
  • 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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment