Recent posts

Installing Mopidy with Spotify and Iris

What is MPD and Mopidy?

The Music Player Daemon is a open-source and free music player server without a interface. In order to use in you need a client, we will be using Iris a web UI plugin for Mopidy. With the right setup this allows us to control the music over the network. To be a bit more exact we will be using Mopidy, a Python based server, which is compatible with Spotify as well as local and streaming sources(SoundCloud, Google Play Music and more). It's very extensible using Python PIP to install plugins.

What do we need

UPDATE: This works on a Raspberry PI as well.

The most important thing is a PC running Ubuntu with internet and access to the terminal or SSH. Some basic knowledge on using basic terminal functions is advised. We will also need a Spotify Premium account for this to work.

Setting up the repo and installing

  1. Add the repo's GPG key:
    wget -q -O - https://apt.mopidy.com/mopidy.gpg | sudo apt-key add -
  2. Add the repo to your sources
    sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/buster.list
  3. Install Mopidy, Mopidy-Spotify and Mopidy-MPD
    sudo apt update && sudo apt install mopidy mopidy-spotify mopidy-mpd
  4. Install Python's PIP3 to get Mopidy-Iris
    sudo apt install python3-pip
  5. Now we can use this tool to get the web UI extension
    sudo python3 -m pip install Mopidy-Iris

If you had no error's or problems during all installs we are now ready to start configuring Mopidy.

Configuring everything

Depending if you want to run Mopidy as a service or on a useraccount the config file location will change. If you run it as a service the configs location would be:

/etc/mopidy/mopidy.conf

And if you run it on your user account it will be:

~/.config/mopidy/mopidy.conf

I will not be running Mopidy as a service and just start it from a terminal window when I want to use it. So that's what my guide focuses on. If you want to run it as a service check out the Mopidy documentation here. First we need a bit of information from Spotify, a client id and secret. We can obtain these using a special page made by the guys over at Mopidy. https://mopidy.com/ext/spotify/ This will provide you with the first thing we need to add to our config, the client_id and client_secret, we just need to add a few more value's to our spotify section in the config, afterwards it should look something like this.

[spotify]
username = your_username
password = your_password
client_id = your_client_id
client_secret = your_client_secret

Some other interesting settings we can change in this section are:

  • bitrate - Audio bitrate in kbps. 96, 160, or 320. Defaults to 160. Higher is better.
  • volume_normalization - Whether volume normalization is active or not. Defaults to true.
  • toplist_countries - Comma separated list of two letter ISO country codes to get toplists for. Defaults to blank, which is interpreted as all countries that Spotify is available in.

Even tough it not needed I suggest setting the locale and country for Iris to your country as it defaults to AU. I live in the Netherlands and prefer software to be in english so I set mine as:

[iris]
country = nl
locale = en_US

If you would now run Mopidy everything should already be working just fine. To test it you can load the web interface in a browser located at: http://localhost:6680/, if Iris is properly installed you should be able to open it from there.

Extra settings

Here are a few settings to might be interesting to you:

[http]
hostname = ::
default_app = iris

The hostname setting makes the WebUI accessible from other devices in your network, by default only the localhost can access it. And the default_app make sure to load you straight into Iris when accessing the WebUI.

.bashrc More

Well here's one more, this one swaps the names of 2 files.

function swap()
{
    local TMPFILE=tmp.$$
    mv "$1" $TMPFILE
    mv "$2" "$1"
    mv $TMPFILE "$2"
}

The next one changes a filename to all lowercase characters.

function lowercase()
{
    for file ; do
        filename=${file##*/}
        case "$filename" in
            */*) dirname==${file%/*} ;;
            *) dirname=.;;
        esac
        nf=$(echo $filename | tr A-Z a-z)
        newname="${dirname}/${nf}"
        if [ "$nf" != "$filename" ]; then
            mv "$file" "$newname"
            echo "lowercase: $file --> $newname"
        else
            echo "lowercase: $file not changed."
        fi
    done
}

And a function to find a file using part of its filename

function find_file() { find . -type f -iname '*'$*'*' -ls ; }

Oh and I added Prism for syntax highlighting!

.bashrc Stuff

Might as well share some .bashrc additions I like to use. First up a simple function to extract most compressed files.

function extract {
    if [ -z "$1" ]; then
        # display usage if no parameters given
        echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
    else
        if [ -f $1 ] ; then
            # NAME=${1%.*}
            # mkdir $NAME && cd $NAME
            case $1 in
                *.tar.bz2)   tar xvjf ../$1    ;;
                *.tar.gz)    tar xvzf ../$1    ;;
                *.tar.xz)    tar xvJf ../$1    ;;
                *.lzma)      unlzma ../$1      ;;
                *.bz2)       bunzip2 ../$1     ;;
                *.rar)       unrar x -ad ../$1 ;;
                *.gz)        gunzip ../$1      ;;
                *.tar)       tar xvf ../$1     ;;
                *.tbz2)      tar xvjf ../$1    ;;
                *.tgz)       tar xvzf ../$1    ;;
                *.zip)       unzip ../$1       ;;
                *.Z)         uncompress ../$1  ;;
                *.7z)        7z x ../$1        ;;
                *.xz)        unxz ../$1        ;;
                *.exe)       cabextract ../$1  ;;
                *)           echo "extract: '$1' - unknown archive method" ;;
            esac
        else
            echo "$1 - file does not exist"
        fi
    fi
}

Also here is a simple way to check if a user is root, and if the user is not call something. I used it add sudo to apt-get and apt when the user is not a root.

if [[ $EUID -ne 0 ]] ; then
    alias apt-get='sudo apt-get'
    alias apt='sudo apt'
fi

If I start doing alot of code or script posting I might add some highlighting system to this.

Test

This is just a simple test of this blog system. I might post some stuff here.