Spotify playback and volume control using keyboard shortcuts on Linux

Abstract

Recently I've been playing around with my new Ubuntu setup (using bspwm, seen below) and I needed to get my keyboard playback and volume controls working to use Spotify efficiently. While this topic has been covered extensively on the web, the knowledge is currently spread among multiple websites and this is my attempt to gather some parts of it in one place so it'll be easier for you to start off. Obviously the exact setups will vary greatly depending on your setup, but I have this article will be able to give you a starting point.

Tim's Ubuntu 16.10 setup using bspwm

A quick clarification: In this article I'll be talking about designated volume control keys, such as the ones below as they appear on my K70. That said, you can potentially map volume/playback controls to any key you want, e.g. F11 or Super + q.

Corsair K70 volume control keys

Keyboard volume controls

First of all, the keys you're interested in are XF86AudioRaiseVolume, XF86AudioLowerVolume and XF86AudioMute.

To find which commands to bind them to, you must know what you use for sound control in your system. I personally use PulseAudio so in my case pactl does the job, while you might use something like ALSA.

I also use sxhkd to handle all of the bindings, and below you can see what I had to add to my sxhkdrc. I set the volume increment to 5% but you can change it to whatever works for you.

# Structure is:
# <keyboard-key>
#     <command-to-bind>
XF86AudioMute  
    pactl set-sink-mute 0 toggle
XF86AudioRaiseVolume  
    pactl set-sink-volume 0 +5%
XF86AudioLowerVolume  
    pactl set-sink-volume 0 -5%;

Note that this will adjust volume for the whole system, not just Spotify. I looked into adjusting Spotify volume using the approach described below, and sadly it doesn't seem to be possible at the time of writing.

Keyboard Spotify controls

There are several ways to get keyboard controls to work with Spotify Linux client, I went with MPRIS2 + D-Bus combination. I'm running Ubuntu 16.10 so I had to install a couple of dependencies (sudo apt-get install mpdris2 mpris-remote), but once this was out of the way this addition to my sxhkdrc did the trick:

XF86AudioPlay  
    dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
XF86AudioStop  
    dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop
XF86AudioNext  
    dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next
XF86AudioPrev  
    dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous

You can also use this little hacky command for XF86AudioPlay to launch Spotify when you press play and Spotify is not open (just don't spam it):

XF86AudioPlay  
    if ! dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause; then spotify; fi

As I said before, these exact steps might not work for you but I hope I've pointed in the right direction.

If you found this post useful, feel free to like and share:

Comments