Tag Archives: MPD

Spotify on Raspberry Pi ( MPD )

Hello! So I got famous Raspberry Pi B+ on Christmas. First thing I wanted to do with it was setting up so-called “Home Media Center”. The thing is the device I like to use for listening to music is on the other side of my room, so I had to plug-in my phone to mini-jack. After all, phone is not the best device to use it as a playlist provider because every time you get a call you have to get up and pick it up turning off the music. The other thing is that i don’t like to see cables all around my bedroom. So, long story short I started my investigation on how to set up raspberry pi to be able to play music from my Spotify account.

My Raspi runs Wheezy Raspbian distro and it is connected to my local network via WLAN. After a quick research I already knew that there is no ready-to-use Spotify client for Raspbian available, so i had to find some workaround. Finally I went for Mopidy as it turned out to be really user-friendly and installation steps on Raspbian are clear and easy.

What are the common steps to install and successfully run Mopidy daemon? Following the steps in Mopidy docs is essential. Here I provide console commands from Mopidy docs with a few comments from my own experience.

    1. Installing Mopidy
      • It is recommended to set IPv6 support on Raspi as well as add it to boot setup.
        sudo modprobe ipv6
        echo ipv6 | sudo tee -a /etc/modules
      • Add Mopidy archive GPG key
        wget -q -O - https://apt.mopidy.com/mopidy.gpg | sudo apt-key add -
      • Add repositories to your apt-get lookup list
        sudo nano /etc/apt/sources.list
        # Mopidy APT archive
        deb http://apt.mopidy.com/ stable main contrib non-free
        deb-src http://apt.mopidy.com/ stable main contrib non-free
      • Update your system and download mopidy
        sudo apt-get update && sudo apt-get install mopidy
      • Download Spotify extension for Mopidy
        sudo apt-get install mopidy-spotify

Now you should have installed working mopidy daemon. It needs a little configuration ( ie. Spotify user/pass details ). The thing to notice is a little mess in configuration files. Mainly there are two important configuration files: in your ~/.config/mopidy/ dir and in /etc/mopidy/ dir. First one is used when you start mopidy as a standard process of your user, while second is used when started as a daemon. I recommend to just have the same configuration in both files.

  1. Configure Mopidy.
    • Check your device IP address
      ifconfig
    • open configuration file
      sudo nano ~/.config/mopidy/mopidy.conf
    • If you want to use MPD applications for your smartphone to manage your playlists set MPD configuration – host ( wlan0 in my case ), port and password
      [mpd]
      enabled = true
      hostname = 192.168.1.5 #wlan0 ip
      port = 6600
      password = *setPass*
    • If you want to use http client to manage your playlists set HTTP configuration.
      [http]
      enabled = true
      hostname = 192.168.1.5
      port = 6680
    • set spotify user configuration (see comment below)
      [ spotify ]
      enabled = true
      username = *yourSpotifyUsername*
      password = *yourSpotifyPass*
      bitrate = 320
      If you login to Spotify using Facebook account you have to create username/pass for your devices on your Spotify account settings. To do so, go to https://www.spotify.com/pl/account/set-device-password/ and follow the instructions (send email to your email account with a link to set password)
  2. Now save your configuration file and check if everything works fine. To do so run in console:
    mopidy
    You should see a few warnings probably about your configuration of local libraries (used when you want to play music from your Raspi local disk). After a while there should be also a notification that ‘xy playlists imported from Spotify’. If you see such information then you know you have done everything properly. You can kill your process now. To run your daemon service with the same configuration you have to copy config file from ~/.config/mopidy/mopidy.conf to /etc/mopidy/ directory (backup existing file first).
    sudo cp /etc/mopidy/mopidy.conf /etc/mopidy/mopidy.conf_backup && sudo cp ~/.config/mopidy/mopidy.conf /etc/mopidy/mopidy.conf
  3. You probably want to run your service on Raspi startup so you don’t have to think about this anymore. When installing the most recent Mopidy there should be a script for this already installed. see:
    cat /etc/init.d/mopidy
    In case you see nothing there, here is the script I had predefined and it works fine:

    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides: mopidy
    # Required-Start: $network $remote_fs
    # Required-Stop: $network $remote_fs
    # Should-Start: $named alsa-utils avahi dbus pulseaudio
    # Should-Stop: $named alsa-utils avahi dbus pulseaudio
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Mopidy music server
    ### END INIT INFO

    PATH=/sbin:/usr/sbin:/bin:/usr/bin
    DESC="Mopidy music server"
    NAME=mopidy
    DAEMON=/usr/bin/mopidy
    DAEMON_USER=mopidy
    DAEMON_GROUP=audio
    CONFIG_FILES="/usr/share/mopidy/conf.d:/etc/mopidy/mopidy.conf"
    PIDFILE=/var/run/$NAME.pid
    SCRIPTNAME=/etc/init.d/$NAME

    # Exit if the package is not installed
    [ -x $DAEMON ] || exit 0

    # Read configuration variable file if present
    [ -r /etc/default/$NAME ] && . /etc/default/$NAME

    # Load the VERBOSE setting and other rcS variables
    . /lib/init/vars.sh

    # Define LSB log_* functions.
    . /lib/lsb/init-functions

    do_start()
    {
    start-stop-daemon --start --quiet --name $NAME --pidfile $PIDFILE \
    --startas $DAEMON --test > /dev/null \
    || return 1
    start-stop-daemon --start --quiet --name $NAME --pidfile $PIDFILE \
    --chuid $DAEMON_USER:$DAEMON_GROUP --background --make-pidfile \
    --startas $DAEMON -- --quiet --config $CONFIG_FILES \
    || return 2
    }

    do_stop()
    {
    start-stop-daemon --stop --quiet --name $NAME --pidfile $PIDFILE \
    --retry=TERM/30/KILL/5
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # This typically happens with pulseaudio.
    start-stop-daemon --stop --quiet --oknodo --user $DAEMON_USER \
    --retry=TERM/30/KILL/5
    [ "$?" = 2 ] && return 2
    rm -f $PIDFILE
    return "$RETVAL"
    }

    # Remove the action from $@ before it is used by the run action
    action=$1
    [ "$action" != "" ] && shift

    case "$action" in
    start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC " "$NAME"
    do_start
    case "$?" in
    0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
    2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
    stop)
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
    0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
    2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
    status)
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
    restart|force-reload)
    log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
    0|1)
    do_start
    case "$?" in
    0) log_end_msg 0 ;;
    1) log_end_msg 1 ;; # Old process is still running
    *) log_end_msg 1 ;; # Failed to start
    esac
    ;;
    *)
    # Failed to stop
    log_end_msg 1
    ;;
    esac
    ;;
    run)
    echo -n "\"service mopidy run\" is deprecated. " 1>&2
    echo "Use \"mopidyctl\" instead." 1>&2
    /usr/sbin/mopidyctl $@
    ;;
    *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload|run}" >&2
    exit 3
    ;;
    esac

    :

    Now the last thing to do is to notify system to execute this script on boot:
    sudo update-rc.d mopidy defaults

  4. Installing Spotify for Mopidy Http client (for more clients see Mopidy docs)
    • You need python-pip package to be able to easily install Mopidy-Spotify extension for your daemon.
      sudo apt-get install python-pip build-essential
    • Now you are only one step away from browsing your playlists from Spotify using Raspi:
      pip install Mopidy-Mopify

…and this is it. Now in your browser you can go to http://’device_ip’:’configured_port’/mopify/ (in my case http://192.168.1.5:6680/mopify/), plug mini jack from your home device to Raspi and enjoy your music!

Feel free to post any comments and questions if I have missed something in this tutorial and you if you have any problems with setting it up.
Cheers!