
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
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
Write on December 28th, 2010 at 10:52 pm
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. ;)
Write on January 21st, 2011 at 3:52 pm
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.
Write on January 29th, 2011 at 3:33 pm
Blacklisting d’artistes sur Spotify en mode radio (natif linux)…
En tout cas, merci au site Mashibu pour m’avoir guidé sur la bonne voie et me permettre d’écouter sereinement ma radio metal sur spotify sans, entre autres, ce satané Nickelback ! ;-)…
Write on March 31st, 2011 at 4:35 pm
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!
Write on June 27th, 2011 at 12:18 pm
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)
Write on June 27th, 2011 at 12:20 pm
@richard thanks for your read!
Write on July 5th, 2011 at 11:11 am
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 :)
Write on August 23rd, 2011 at 6:05 pm
[...] get the multimedia keys working, we need to follow the instructions in this tutorial and the codes described at [...]
Write on December 11th, 2011 at 3:34 pm
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!
Write on December 15th, 2011 at 6:30 pm
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!
Write on January 9th, 2012 at 4:41 pm