Playing with D-Bus interface of Spotify for Linux

After a lot of requests from Linux users, Spotify developers have integrated D-Bus support in version 0.4.8.282. So, what this means is simply and awesome! Now Linux developers could use this programmatic interface to interact with Spotify from other apps.

In other words, now is quite simple to send «play», «pause», «move next/previous song» events to Spotify and with this get Spotify fully integrate into our desktop.

I will explain how to introspect D-Bus interface and make some proof-concepts.

Take care that Spotify Linux client now implements to MPRIS2 Dbus support and almost the entire available libraries of D-Bus could interact with it.

So now we play around to “play” with D-Bus API of Spotify. But first of all we have to install one simple package (I’m using Ubuntu Maverick Merkat, take care of your linux distribution and version to find differences).

Now let’s find what is the Spotify D-Bus’s initial interface.

$ mdbus2|grep spotify
com.spotify.qt
org.mpris.MediaPlayer2.spotify

So, as you could see there are two interfaces to talk with, but looks like the second one is our desired central point. Let’s introspect it…

$ mdbus2 org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2
[METHOD]    org.mpris.MediaPlayer2.Raise() -> ()
[METHOD]    org.mpris.MediaPlayer2.Quit() -> ()
[PROPERTY]  org.mpris.MediaPlayer2.read( b:CanQuit )
[PROPERTY]  org.mpris.MediaPlayer2.read( b:CanRaise )
[PROPERTY]  org.mpris.MediaPlayer2.read( b:HasTrackList )
[PROPERTY]  org.mpris.MediaPlayer2.read( s:Identity )
[PROPERTY]  org.mpris.MediaPlayer2.read( s:DesktopEntry )
[PROPERTY]  org.mpris.MediaPlayer2.read( as:SupportedUriSchemes )
[PROPERTY]  org.mpris.MediaPlayer2.read( as:SupportedMimeTypes )
[METHOD]    org.mpris.MediaPlayer2.Player.Next() -> ()
[METHOD]    org.mpris.MediaPlayer2.Player.Previous() -> ()
[METHOD]    org.mpris.MediaPlayer2.Player.Pause() -> ()
[METHOD]    org.mpris.MediaPlayer2.Player.PlayPause() -> ()
[METHOD]    org.mpris.MediaPlayer2.Player.Stop() -> ()
[METHOD]    org.mpris.MediaPlayer2.Player.Play() -> ()
[METHOD]    org.mpris.MediaPlayer2.Player.Seek( x:Offset ) -> ()
[METHOD]    org.mpris.MediaPlayer2.Player.SetPosition( o:TrackId, x:Position ) -> ()
[METHOD]    org.mpris.MediaPlayer2.Player.OpenUri( s:none ) -> ()
[SIGNAL]    org.mpris.MediaPlayer2.Player.Seeked( x:Position )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( s:PlaybackStatus )
[PROPERTY]  org.mpris.MediaPlayer2.Player.readwrite( s:LoopStatus )
[PROPERTY]  org.mpris.MediaPlayer2.Player.readwrite( d:Rate )
[PROPERTY]  org.mpris.MediaPlayer2.Player.readwrite( b:Shuffle )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( a{sv}:Metadata )
[PROPERTY]  org.mpris.MediaPlayer2.Player.readwrite( d:Volume )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( x:Position )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( d:MinimumRate )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( d:MaximumRate )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( b:CanGoNext )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( b:CanGoPrevious )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( b:CanPlay )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( b:CanPause )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( b:CanSeek )
[PROPERTY]  org.mpris.MediaPlayer2.Player.read( b:CanControl )
[METHOD]    org.freedesktop.DBus.Properties.Get( s:interface_name, s:property_name ) -> ( v:value )
[METHOD]    org.freedesktop.DBus.Properties.Set( s:interface_name, s:property_name, v:value ) -> ()
[METHOD]    org.freedesktop.DBus.Properties.GetAll( s:interface_name ) -> ( a{sv}:values )
[METHOD]    org.freedesktop.DBus.Introspectable.Introspect() -> ( s:xml_data )

Interesting, isn’t it? It have a large D-Bus API to send events to Spotify. Among methods to control the playlist, one of my favourites is get MetaData from the playing song.
So now if you want to pause your actual song of Spotify you can issue at your terminal:

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause

The next steps for Ubuntu users is use this API to integrate indicator-sound and get fully integrated Spotify in our system.
I’m looking forward Spotify developers to integrate LibNotify and show track information with desktop notifications.

Happy hacking!

UPDATE:

For playing songs from a spotify url just issue the next command:

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri string:spotify:track:6JfGHYsw9LVH5FJMCh66Um

UPDATE 2
John Reese has created a gorgeous wrapper for integrating Spotify with media keys by taking the above explanations. Check it out http://noswap.com/projects/spotify-gnome

UPDATE 3

Exists a better way to connect Spotify with GNOME-shell, recently it was published a new extension for integrate Spotify with GNOME. https://extensions.gnome.org/extension/695/mediakeys-to-mpris2/.

And according to the Spotify community forum in the coming months it could be a native integration. The Spotify for Linux client changelog says the next line:  “Linux only: Media key support in Gnome. Coming soon!”

14 Comments

  1. Nick

    Great! I now finally was able to add nice shortcuts using the Compiz Config Settings Manager and add thing like:
    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

  2. Mattias

    I’ve gotten play/pause through mediabuttons, ssh and webserver working and i LOVE it! One thing however I have not figured out, and that is how to use org.mpris.MediaPlayer2.Player.OpenUri, my guess is that it should allow me to start a specific song/playlist. If you figure it out I’d be delighted to read about it. ;)

  3. Fran Diéguez

    Hi Mattias,

    This is quite simple!! Just issue in your terminal the next command:

    dbus-send –print-reply –dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri string:spotify:track:6JfGHYsw9LVH5FJMCh66Um

    Look at the right way of passing parameters with dbus.

  4. Richard Reijmers

    Flippin’ great, finally a good how-to about dbus. The original dbus documentation lacks a proper example on how to implement this in real life, great work!

  5. Richard Reijmers

    I combined the commands with AutoKey to make keyboard shortcuts to skip, play, pause etc.

    system.exec_command(“dbus-send –print-reply –dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Play”, getOutput=False)

  6. Fran Diéguez

    @richard thanks for your read!

  7. Jonathan Trengrove

    This is just what I’ve been looking for! :)
    Really good to have global keyboard shortcuts for native Spotify now.

    Gotta love the Linux community :)

  8. OJ

    This is great. I’m no D-Bus expert, and I have a question.

    How do I check if shuffle is on, and how do I toggle it when:

    [PROPERTY] org.mpris.MediaPlayer2.Player.readwrite( b:Shuffle )

    Thanks!

  9. oj88

    Spotify + D-Bus is great.

    I have used it to write this web-based remote control for Spotify for Linux:
    http://code.google.com/p/spotcommander/

    Screenshots and video available.

    Works great!

  10. Ruhan

    Incredible feature. I put it in my custom shortcuts to next/previous and pause musics.

  11. Daniel W

    haha great song choice dude!
    Shortcut >> commence *le sexytime :P

  12. Thor

    I was wondering if it is possible to start a specific playlist from the terminal? I am working on a alarm_clock in python and would like to wake up to a specific spotify playlist.

Trackbacks & Pingbacks

  1. Let's see where this leads...
  2. Terence Eden has a Blog » Linux Spotify Keybindings

Leave a Reply