Interfacing Banshee 1.0 with DBus and Python

10-06-2008 Alec Hussey 3 Comments

Ever since the DBUS API change (more like overhaul) during the development of Banshee 1.0 and developers haven’t yet started supporting it in their plugins so I decided that I would play around with it. From what I have seen, it seems that the perception is that DBus is hard and complicated but its actually really easy and makes things very simple. Essentially you use a DBus debugger (because in most cases, documentation for an applications’ DBus API is not available) like D-Feet to look up which interfaces, methods, properties, and signals are available to you. Then use them to do what you want.

Bansshe DBus API in D-Feet

Now all we have to do is make a connection to Banshee via DBus and start calling methods. For this we will just use an interactive python interpreter which can be accessed by simply typing “python” at any command line prompt. Firstly, we will import the DBuslibrary.

import dbus

Next, we will make a connection to the session bus.

bus = dbus.SessionBus()

Then we will get Banshee’s PlayerEngine object which will let us control the player itself (Play, Pause, Next, Previous, Current Track, etc).

banshee = bus.get_object("org.bansheeproject.Banshee", "/org/bansheeproject/Banshee/PlayerEngine")

The first argument we pass, “org.bansheeproject.Banshee”, is the interface we want to connect to and “/org/bansheeproject/Banshee/PlayerEngine” is the path to the object that we want to use. Now we can start doing stuff with Banshee. You can pretty much take any of the methods listed in D-Feet and run them. For example:

banshee.Open("file:///home/maddog39/Music/Eisbrecher - Antik├Ârper.mp3")

And to play the song…

banshee.Play()

Once you have had enough….

banshee.Pause()

Although note that you dont have to using the Open() method to use the Play() and Pause() methods, but Open() lets you open an arbitrary media file otherwise those other methods use the currently playing track. Now, to access properties you generally just take the name of the property and prepend ‘Get’ to it and make it a method. So for example:

banshee.GetCanPause()

Returns:

dbus.Boolean(False)

Or if we wanted to display all the information about the currently playing track we might do…

for key in currentTrack.keys():
	print key + ": " + str(currentTrack[key])

Which gives us:

album: Issues
rating: 0
name: Falling Away From Me
artist: Korn
URI: file:///home/maddog39/Music/Korn%20-%20Beating%20Me%20Down.mp3
length: 271.211
disc: 0
track-count: 16
year: 1999
track-number: 2

Another cool thing you can do with DBus is receive signals. What this allows us to do is run code when a given signal is triggered or received. We do this by connecting a python method with a signal on our bus. So if we wanted to do something every time Banshee changes its state (playing, paused, loading, loaded, etc) this is what would happen. We would first have to setup a main loop as it is required for a signals to function.

import gobject
from dbus.mainloop.glib import DBusGMainLoop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

Then we would create a callback method which would be run every time the signal is triggered.

def stateChanged(state):
	print "State Changed: %s" % state

Now we can add our signal receiver to our method.

bus.add_signal_receiver(stateChanged,
dbus_interface="org.bansheeproject.Banshee.PlayerEngine",
                        signal_name="StateChanged")

Finally, at the end of a script that uses signals we would want to run our main loop.

gobject.MainLoop().run()

Well thats pretty much it! Just keep in mind that this was not intended to be a tutorial but merely a guideline on how to do interact and do things with Banshee over DBus. So feel free to sit down and write some of your own scripts based on what you have learned here and I hope this helps. I have already written my own program to integrate Banshee into another application which I will talk about in another article. Enjoy!

Filed under:
Tags: , , ,

3 Responses to “Interfacing Banshee 1.0 with DBus and Python”

  1. Thx a lot for this!!
    Just a (maybe) stupid question:
    I would like to just fetch the current state of banshee (wether it is PLAY, PAUSE STOP)
    How would I do that?

     
  2. got it myself.
    :)
    I am using:

    #————
    #!/usr/bin/python
    import dbus
    bus = dbus.SessionBus()
    banshee = bus.get_object(“org.bansheeproject.Banshee”, “/org/bansheeproject/Banshee/PlayerEngine”)
    status = banshee.GetCurrentState()
    print status
    #————-
    thx a lot!

     
  3. @produnis Sorry havent been around lately. But for future reference, if you haven’t already, try using a DBus debugger to find/list the available methods for a given application.DFeet comes immediately to mind.

    http://live.gnome.org/DFeet/

     

Leave a comment