Skip to content

Instantly share code, notes, and snippets.

@bmaupin
Last active April 17, 2023 20:07
Show Gist options
  • Save bmaupin/acc566ff44a8ebf20c2aa2707789e6ea to your computer and use it in GitHub Desktop.
Save bmaupin/acc566ff44a8ebf20c2aa2707789e6ea to your computer and use it in GitHub Desktop.
Configure multimedia keys for Spotify in Xfce

See also:

First Steps

  1. Open Spotify

  2. Test play/pause key in case it works already

  3. Make sure MPRIS is working by testing play/pause
    (Seems to be working by default for Xubuntu 14.04 and 16.04)

    dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
    
  4. If MPRIS is working, make sure multimedia keys are correctly configured for X

    xev | awk -F'[ )]+' '/^KeyPress/ { a[NR+2] } NR in a { printf "%-3s %s\n", $5, $8 }'
    

    You should see output like this:

    172 XF86AudioPlay
    171 XF86AudioNext
    173 XF86AudioPrev
    

    If you don't see outputs for a particular key, odds are that key is not correctly configured for X

Configure multimedia keys for X server

Skip this section if all of the multimedia keys are showing up appropriately in xev. Otherwise:

  1. See if any other applications are grabbing the multimedia keys (https://unix.stackexchange.com/a/261383/14436)

  2. Open a terminal window and tail the X log

    tail -F /var/log/Xorg.0.log
    
  3. Simulate a press of the multimedia key (change KEY as necessary)

    KEY=XF86AudioNext; xdotool keydown ${KEY}; xdotool key XF86LogGrabInfo; xdotool keyup ${KEY} 
    
  4. Look at the X log to see if any other applications have grabbed the keys

    [  6075.366]       client pid 4231 /opt/google/chrome/chrome --disable-gpu
    
(Google Chrome has grabbed the key in this instance; the solution is simply a matter of closing Google Chrome)

Add multimedia key bindings for Spotify

For each multimedia key that's correctly configured:
(This can also be done manually through SettingsKeyboardApplication Shortcuts)

xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/XF86AudioPlay -s "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause" -n -t string
xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/XF86AudioNext -s "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next" -n -t string
xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom/XF86AudioPrev -s "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous" -n -t string

Using Ansible:

- name: Set up multimedia keys for Spotify
  shell: >
    xfconf-query -c xfce4-keyboard-shortcuts -p '{{ item.property }}' | grep '{{ item.value }}' ||
    xfconf-query -c xfce4-keyboard-shortcuts -p '{{ item.property }}' -t '{{ item.type }}' -s '{{ item.value }}'
  with_items:
    - { property: "/commands/custom/XF86AudioNext", type: string, value: "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next" }
    - { property: "/commands/custom/XF86AudioPlay", type: string, value: "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause" }
    - { property: "/commands/custom/XF86AudioPrev", type: string, value: "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous" }
  register: result
  changed_when: result.stdout.find(item.value) == -1
@aquohn
Copy link

aquohn commented Apr 17, 2023

A more robust and simpler alternative might be to use a small program like playerctl, which manages multiple MPRIS applications and decides which one to send the commands to (based on which was most recently interacted with). More information about MPRIS can be found on the Arch Wiki.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment