Basic control of amarok2 through DBus in PHP – napping again.

Posted on June 3, 2009

1


For my productivity naps I used to let the amarok script weekalarm wake me up after a certain number of minutes. Unfortunately amarok2 doesn’t seem to work with the script anymore. So I was looking for another way to tell amarok to start playing after a certain amount of time.

So some Google research led me to remote controlling applications through DCOP which is the (old!) inter process communication system of KDE. I found out that amarok could be remote controlled using certain DCOP commands. A short

dcop

however showed that amarok was not listening to DCOP messages. For a good reason: DCOP was replaced by DBus in KDE4. Regarding DBus debugging d-feet worked well for me.

You can simply use the command line to send DBus messages. The command to let amarok start playing would like something like that:

dbus-send --type=method_call --dest=org.kde.amarok /Player org.freedesktop.MediaPlayer.Play

I was looking further for a native programming language implementation of DBus.

After some (installation!) problems with the ruby library rbus I switched to PHP using Gree Labs PHP DBus. They also have a short tutorial which is a nice start on the matter.

Little tweek of the example code lets amarok start playing after a given number of minutes and increasing the volume continuously:

$START_VOLUME = 10;
$VOLUME_STEP = 2;

function execute($command, $args, $dbus) {
	$m = new DBusMessage(DBUS_MESSAGE_TYPE_METHOD_CALL);
	$m->setDestination("org.kde.amarok");
	$m->setInterface("org.freedesktop.MediaPlayer");
	$m->setPath("/Player");
	$m->setMember($command);
	if($args !== null) {
		$m->appendArgs($args);
	}
	$dbus->send($m);
}

$minutes = intval($argv[1]);
sleep($minutes * 60);

$type = DBUS_BUS_SESSION;
$dbus = dbus_bus_get($type);

execute("VolumeSet", $START_VOLUME, $dbus);
execute("Play", null, $dbus);

for($count = $START_VOLUME + $VOLUME_STEP; $count < 100; $count += $VOLUME_STEP) {
	execute("VolumeSet", $count, $dbus);
	sleep(1);
}

Amarok is following the MPRIS specification, so for reading on more commands I can recommend the MPRIS specs.

Update: More command line dbus commands for amarok can be found here.

Advertisements
Tagged: , , ,
Posted in: Uncategorized