Spotify playback and volume control using keyboard shortcuts on Linux

Published on February 18, 2017
under Blog

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.

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.

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.

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: