# Spotify

Spotify is a digital music service that gives you access to millions of songs.

This Internet music service allows you to select any song in its database and stream for free. The service was recently introduced to the United States after previously being exclusive to Europe. The Linux client is only officially packaged for Debian and Fedora distributions, but is also available in the AUR: spotify. Officially, they recommend that Linux users run the windows client under Wine. There are also the occasional voice ads in between songs for users who do not wish to subscribe.

Spotify also offers free users the ability to create playlist which can be shuffled, and set to repeat tracks. Content provided by Spotify comes in explicit versions as well as censored.

## Installing The Client

Choose which client you would prefer. The Linux client is receiving good reviews. However, if you are comfortable with wine and it's configuration, you might want to choose the windows client. Please note that you do NOT need to install both.

### Installation of Linux Client

Follow the instructions for installing packages from the AUR: https://wiki.archlinux.org/index.php/AUR#Installing_packages and install the AUR package here: spotify. The pkgbuild will automatically download the software. It would be a good use of time to go to the spotify website and create your user account while it's building. If you wish to play local files you will need to install ffmpeg-spotify as well. You can also install spotify-gnome-git to control spotify with gnome's media keys.

### Installation of Windows Client using Wine

#### Install Wine

First, you must ensure that you have Wine installed on your system. In addition to installing Wine you will need to take a moment to configure it for the user who will be running the application.

Depending on your choice of architecture it may be necessary for you to enable the multilib repositories. This is necessary to install Wine on x86_64 systems, if not enabled pacman will inform you that the package was not found.

    # pacman -Syy
    # pacman -S wine wine_gecko

If you are using x86_64, you also need to install the lib32-alsa-lib and lib32-mpg123 packages.

    # pacman -S lib32-alsa-lib lib32-mpg123

#### Run Installer

Obtaining Spotify can be done by registering for an account on their Website, the application does not offer in-app registration.

However you can obtain the application prior to registering by using the following URL. [1]

After you have registered and downloaded your copy of the installer you will need to run the application through Wine, depending on your setup you may be able to run the application by right clicking the file. If not terminal will work just fine, as long as you run the below command in the directory of your download.

    $ wine Spotify\ Installer.exe

Once the application is successfully installed you may run Spotify by using one of the following commands in terminal, or in the ALT+F2 launcher:

If you use a x86_64 copy of ArchLinux, you'll have to run it like this:

    $ wine "/home/username/.wine/drive_c/Program Files (x86)/Spotify/spotify.exe"

If you use a x86 copy of ArchLinux, you can use this command just fine:

    $ wine ~/.wine/drive_c/Program\ Files/Spotify/spotify.exe

If you have any additional problems, I recommend setting the winecfg to Windows XP or Windows 7 emulation.

## Global Media Hotkeys

Spotify has support for media keys like `XF86AudioPlay`, but out of the box they only work inside Spotify. We can use for example xbindkeys to catch the global media keypresses, and then forward them to Spotify using one of the methods below.

### Linux Client

#### Using Playerctl

The playerctl utility provides a command line tool to send commands to the Spotify process. The only two commands you will likely need to bind globally are `play-pause` and `next`

    $ playerctl play-pause
    $ playerctl next

Playerctl will send the command to the first player it finds, so this method will also work with others players such as vlc. To ignore other players, pass `--player=spotify` as an argument.

#### Using a bash-script and xdotool

With the help of `xdotool` it is possible to send your hotkeys to the application. The following script is an example of how to control Spotify from the outside:

    #!/bin/sh

    case $1 in
       "play")
           key="XF86AudioPlay"
           ;;
       "next")
           key="XF86AudioNext"
           ;;
       "prev")
           key="XF86AudioPrev"
           ;;
       *)
           echo "Usage: $0 play|next|prev"
           exit 1
            ;;
    esac
    xdotool key --window $(xdotool search --name "Spotify (Premium |Unlimited )?- Linux Preview"|head -n1) $key
    exit 0

Let's call it `musickeys.sh`. Make the script executable:

    $ chmod +x musickeys.sh

By executing `./musickeys.sh play` you can now toggle playing a song. Now you can bind this script to any tool that catches keypresses, such as xbindkeys. For Openbox you can use the following example (see Openbox#Configuration for help):

    <keybind key="XF86AudioPlay">
      <action name="Execute">
          <execute>~/bin/musickeys.sh play</execute>
      </action>
    </keybind>
    <keybind key="XF86AudioNext">
      <action name="Execute">
          <execute>~/bin/musickeys.sh next</execute>
      </action>
    </keybind>
    <keybind key="XF86AudioPrev">
      <action name="Execute">
          <execute>~/bin/musickeys.sh prev</execute>
      </action>
    </keybind>

#### D-Bus

An alternative to the above is D-Bus, which should be available by default as it is a dependency of systemd. With D-Bus we have a consistent and reliable way to communicate with other processes, such as Spotify. To play or pause the current song in Spotify:

    $ dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause

In order to bind this and the other commands to the media keys you need to install Xbindkeys and edit your .xbindkeysrc and add the following lines:

    # Play/Pause
    "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause"
    XF86AudioPlay

    # Next
    "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next"
    XF86AudioNext

    # Previous
    "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous"
    XF86AudioPrev

    # Stop
    "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop"
    XF86AudioStop

If the above commands don't work, try setting the dbus address:

     USER=`whoami`
     PROCESS=spotify
     PID=`pgrep -o -u $USER $PROCESS`
     ENVIRON=/proc/$PID/environ
     if [ -e $ENVIRON ]
     then
         export `grep -z DBUS_SESSION_BUS_ADDRESS $ENVIRON`
     else
         echo "Unable to set DBUS_SESSION_BUS_ADDRESS."
         exit 1
     fi

### Windows-Client (via wine)

If you prefer the wine-version of Spotify, you can use spotifycmd to send actions to Spotify.

## Tips & Tricks

### Hide Track Info Notifications

After version 0.9.10, track change notifications were enabled by default. They can be quite intrusive. To disable them, add the following line to `~/.config/spotify/Users/<spotifylogin>-user/prefs`

     ui.track_notifications_enabled=false

It is also possible to launch spotify with the `--ui.track_notifications_enabled=false` option.

### Show Track Info Notifications

playerctl provides a library you can use with python-gobject and a notification daemon such as dunst to show the artist and title in a notification when the track changes.

    #!/usr/bin/env python3

    from gi.repository import Playerctl, GLib
    from subprocess import Popen

    player = Playerctl.Player()

    def on_track_change(player, e):
        track_info = '{artist} - {title}'.format(artist=player.get_artist(), title=player.get_title())
        Popen(['notify-send', track_info])

    player.on('metadata', on_track_change)

    GLib.MainLoop().run()

### Skip Overplayed Radio Tracks

Another use of the playerctl library is to skip tracks that are played too much on radio when you do not necessarily want to downvote these tracks because you may want to hear them again later on that station.

    #!/usr/bin/env python3

    from gi.repository import Playerctl, GLib

    player = Playerctl.Player()

    played_out = ['Zu Fuss', 'Walk And Talk', 'Neuland']

    def on_track_change(player, e):
        if player.get_title() in played_out:
            player.next()

    player.on('metadata', on_track_change)

    GLib.MainLoop().run()

### Mute Commercials

With blockify you can mute commercials (works on both the Wine version and the native Linux client). It is available in the AUR as blockify.

### Remote Control

#### Send Commands via SSH

If you set up ssh on the server, you can send controls from a client to a remote Spotify instance with

    $ ssh user@host 'yourcommand'

where yourcommand can be spotifycmd that you installed on the server or a dbus script for the linux version, as described above.

#### Grab the Spotify Window via SSH

Aside from grabbing the whole desktop with TeamViewer or VNC to remotely control your server, you can also only grab the Spotify Window from the server to your client.

To do that, you need to configure sshd on your server and install x11vnc on both server and client as well as tigervnc on the client. Then you can use these scripts to grab either the complete dektop or only the Spotify window, which essentially gets you GUI client-like behavior as with MPD.

    #!/bin/bash
    # vncget.sh

    if [[ $1 == all ]];then
      ssh -f -t -L 5900:localhost:5900 user@host "x11vnc -q -display :0 -auth .Xauthority"
    else
      ssh -f -t -L 5900:localhost:5900 user@host ".bin/vncgetspotify.sh"
    fi

    for i in {1..4}; do
      sleep 2
      if vncviewer localhost:0; then break; fi
    done

    #!/bin/bash
    # vncgetspotify.sh

    export DISPLAY=:0

    id=$(wmctrl -lx | awk '/spotify.exe.Wine/ {print $1}')
    [[ -z $id ]] && id=$(wmctrl -lx | awk '/spotify.Spotify/ {print $1}')

    x11vnc -sid $id -display :0 -auth .Xauthority

You will need to copy the second script to ~/.bin/vncgetspotify.sh on the server and the first script to any place on your client.

Finally, to grab the spotify window, run on the client:

    $ sh vncget.sh

or, for the whole desktop:

    $ sh vncget.sh all

## Troubleshooting

### Broken radio

  Spotify bug report concerning mixed locales

If your radio page is broken (stuck when starting and unsresponsive to input) you might be using a custom locale. Try setting the environment variable `LC_NUMERIC` to `en_US.utf8` before starting Spotify.

### Spotify won't play local files

This can manifest in a very unusual manner like some songs not playing when you try to stream them because the player attempts to play them from the hard drive.

Try installing ffmpeg-compat, as per this forum discussion.

### SpotifyHelper.exe crashes (Windows client)

If SpotifyHelper.exe crashes when starting Spotify, disable the d3d9 library with `winecfg`. Go to the "Libraries" tab, choose "d3d9" and click Add. To disable it, click edit and select the "Disable" option.

### Sound distorted and sped up when using PulseAudio (Windows client)

If you experience sound distortion and playback issues, try the following fix as per this forum post:

In `/etc/pulse/daemon.conf`, add the following entry:

    default-fragment-size-msec = 5

### Wrong launcher icon (Windows client)

If the Spotify icon does not show up correctly in your launcher, add the following line to `~/.local/share/applications/wine/Programs/Spotify.desktop`:

    StartupWMClass=spotify.exe

### System sound gets muted/altered when Spotify is running (pulseaudio)

If you experience your other application's volumes or overall system volume being altered on its own while running Spotify, disable the cork feature of pulseaudio in `/etc/pulse/default.pa` by commenting out the following line:

    load-module module-role-cork

Then simply restart the pulseaudio server:

    pulseaudio -k
    pulseaudio --start

### Album art and images are missing, show up as squares; with proxy

Quit spotify, then open spotify preferences `~/.config/spotify/prefs`

Change @https to @http:

     network.proxy.addr="your-proxy.com:80@http"
     network.proxy.mode=2

See original form post here.

## See also

- playerctl: A command-line utility and library for controlling media players
- SpotCommander: A web based remote control for Spotify
- http://www.spotify.com/int/help/faq/wine/
- http://www.spotify.com/int/download/previews/

Categories:

- Audio/Video
- Wine