Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spotify - Linux key bindings. From XFCE / Ubuntu keyboard shortcuts configuration, assign the control command to their key. http://shkspr.mobi/blog/2011/12/linux-spotify-keybindings/
"dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause" XF86AudioPlay
"dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop" XF86AudioStop
"dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next" XF86AudioNext
"dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous" XF86AudioPrevious
@PetterJ123
Copy link

PetterJ123 commented Oct 8, 2017

Bless every little part of you, have been searching for this solution in forever! Can't thank you enough! <3

@luperr
Copy link

luperr commented Oct 19, 2017

Thank you very much!

@Endur3
Copy link

Endur3 commented Oct 19, 2017

Awesome, thanks!

@SergioGeeK7
Copy link

SergioGeeK7 commented Oct 25, 2017

great job

@graves501
Copy link

graves501 commented Oct 27, 2017

@liquid2026 worked like a charm, thx!

@Nukencio
Copy link

Nukencio commented Nov 14, 2017

Thanks guys! :D
Working in Xubuntu 16.04.3

@bbrrck
Copy link

bbrrck commented Nov 27, 2017

thanks!

@dsalinux
Copy link

dsalinux commented Dec 4, 2017

Perfect, thanks!

@rust20
Copy link

rust20 commented Jan 17, 2018

Thanks!

@varh1i
Copy link

varh1i commented Jan 21, 2018

Thanks @sjug !

@campanoon
Copy link

campanoon commented Feb 4, 2018

Thanks @jbonney for the command and @sjug for the suggestion!

@andredxc
Copy link

andredxc commented Feb 6, 2018

Still works, thanks!!

@felixhummel
Copy link

felixhummel commented Mar 14, 2018

Thank you very much! :)

@lsl
Copy link

lsl commented Apr 17, 2018

Bindsym version for the i3wm people using media keys (.i3/config)

# Bind media keys to spotify play, stop, next, previous
bindsym XF86AudioPlay exec dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
bindsym XF86AudioStop exec dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop
bindsym XF86AudioNext exec dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next
bindsym XF86AudioPrev exec dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous

@ptaferner
Copy link

ptaferner commented Apr 19, 2018

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

Those work absolutely fine for me on Ubuntu 17.10 when defined as "Custom Shortcuts" via Gnome Settings.

@Dessix
Copy link

Dessix commented May 27, 2018

To improve upon @barnaba's example for I3 users, and for those preferring direct media keys on hardware without fiddling with the Gnome controls UI:

# Pulse Audio controls
bindsym XF86AudioRaiseVolume exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ +5% #increase sound volume
bindsym XF86AudioLowerVolume exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ -5% #decrease sound volume
bindsym XF86AudioMute exec --no-startup-id pactl set-sink-mute @DEFAULT_SINK@ toggle # mute sound

# Media keys

## Using [playerctl](https://github.com/acrisci/playerctl)
bindsym XF86AudioPrev exec --no-startup-id playerctl previous
bindsym XF86AudioNext exec --no-startup-id playerctl next
bindsym XF86AudioPlay exec --no-startup-id playerctl play-pause
bindsym XF86AudioPause exec --no-startup-id playerctl pause
bindsym XF86AudioStop exec --no-startup-id playerctl stop

## Non-playerctl option for Ubuntu
# bindsym XF86AudioPrev exec --no-startup-id "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous"
# bindsym XF86AudioNext exec --no-startup-id "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next"
# bindsym XF86AudioPlay exec --no-startup-id "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause"
# bindsym XF86AudioPause exec --no-startup-id "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause"
# bindsym XF86AudioStop exec --no-startup-id "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Stop"

@h3ct0r
Copy link

h3ct0r commented Jun 29, 2018

Thanks! Worked very well on a DELL 5777 laptop with a vanilla Xubuntu 16.04!

@GarciaSouza
Copy link

GarciaSouza commented Jul 4, 2018

T.K.S!!!
Worked very well on a Lenovo IdeaPad 320 laptop with a Xubuntu 17.10 artful!

@torgeir
Copy link

torgeir commented Sep 8, 2018

To date https://gist.github.com/jbonney/5743509#gistcomment-1422561 is still working perfectly on xfce/ubuntu studio 👏

@chfritz
Copy link

chfritz commented Nov 18, 2018

For anyone curious which other commands the spotify client supports try:

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Introspectable.Introspect

This lists another method "Seek". Unfortunately it doesn't seem to do anything though, because no matter what value I try for Offset, it doesn't have any effect:

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Seek int64:10

Still wanted to leave it here in case anyone else wants to figure this out. Seemed useful.

@arthurmbandeira
Copy link

arthurmbandeira commented Jul 11, 2019

Is there a command for the "15 seconds" key in Spotify?

@AugustoCalaca
Copy link

AugustoCalaca commented Jul 13, 2019

It's work on Debian 10

@andrefsp
Copy link

andrefsp commented Aug 5, 2019

Debian 10 - Buster.

Works just fine.

@chfritz
Copy link

chfritz commented Oct 1, 2019

For anyone else with this issue: In Ubuntu 18.04 using the snap version of Spotify the media keys work out of the box, except that Chrome is a little greedy and captures these keys (presumably to control YouTube content and the like). The good news is that this can be disabled here:
chrome://flags/#hardware-media-key-handling. After doing so the media key events will pass-through to Spotify again.

@mroliveira7
Copy link

mroliveira7 commented Nov 4, 2019

Working great xubuntu 18.04
Thanks a lot!

@kobymeir
Copy link

kobymeir commented Dec 12, 2019

@chfritz 10x so much that helped a lot

@chfritz
Copy link

chfritz commented Jan 22, 2020

And for anyone interested in querying the currently running song, this can be gotten from:

dbus-send --dest=org.mpris.MediaPlayer2.spotify --print-reply /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player string:Metadata

Sample output:

method return time=1579663676.086557 sender=:1.134 -> destination=:1.420 serial=406 reply_serial=2
   variant       array [
         dict entry(
            string "mpris:trackid"
            variant                string "spotify:track:3d8Awbi4CFYp4Wp231MqdS"
         )
         dict entry(
            string "mpris:length"
            variant                uint64 433000000
         )
         dict entry(
            string "mpris:artUrl"
            variant                string "https://open.spotify.com/image/ab67616d00001e02bd3afe3a0169e70901d2933e"
         )
         dict entry(
            string "xesam:album"
            variant                string "Total 7"
         )
         dict entry(
            string "xesam:albumArtist"
            variant                array [
                  string "Various Artists"
               ]
         )
         dict entry(
            string "xesam:artist"
            variant                array [
                  string "Oxia"
               ]
         )
         dict entry(
            string "xesam:autoRating"
            variant                double 0.01
         )
         dict entry(
            string "xesam:discNumber"
            variant                int32 2
         )
         dict entry(
            string "xesam:title"
            variant                string "Domino"
         )
         dict entry(
            string "xesam:trackNumber"
            variant                int32 11
         )
         dict entry(
            string "xesam:url"
            variant                string "https://open.spotify.com/track/3d8Awbi4CFYp4Wp231MqdS"
         )
      ]

@sago104
Copy link

sago104 commented Aug 9, 2020

Thanks for the help! I follow your instructions. Now I can command Spotify in Manjaro KDE just with a little change.

I add this photo for guide in global hotkeys

image

@ilya-bystrov
Copy link

ilya-bystrov commented Dec 5, 2020

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Seek int64:10

Find out that seek value is specified in microseconds. So you have specify int64:1000000 for 1 sec. It works for me.

@brccabral
Copy link

brccabral commented Sep 26, 2021

This works with Spotify, but then YouTube stops working (running from Brave browser). Is there a way to keep both Spotify and YouTube?

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