See also:
-
Open Spotify
-
Test play/pause key in case it works already
-
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
-
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
Skip this section if all of the multimedia keys are showing up appropriately in xev. Otherwise:
-
See if any other applications are grabbing the multimedia keys (https://unix.stackexchange.com/a/261383/14436)
-
Open a terminal window and tail the X log
tail -F /var/log/Xorg.0.log
-
Simulate a press of the multimedia key (change
KEY
as necessary)KEY=XF86AudioNext; xdotool keydown ${KEY}; xdotool key XF86LogGrabInfo; xdotool keyup ${KEY}
-
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)
For each multimedia key that's correctly configured:
(This can also be done manually through Settings → Keyboard → Application 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
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.