XMMS2 PERL API
From Bob's Basement
[edit] Introduction
There seems to be very little documentation regarding the PERL API for XMMS2, so I thought I'd cobble some together!
Before we start we need to do the usual perl customaries
#!/usr/bin/perl use Audio::XMMSClient;
Before we can start issuing commands to XMMS2d we need to connect to it!
#So we open a connection
my $xmms = Audio::XMMSClient->new('tutorial');
#Next we test if the connection failed. If it did, we exit, if it didn't, we carry on.
if (!$xmms->connect) {
printf STDERR "Connection failed: %s\n", $xmms->get_last_error;
exit 1;
}
We are now connected to the XMMS2d so we can start controlling XMMS2. Let's start by getting the numeric ID of the currently playing track.
#First, we get the result
my $result = $xmms->playback_current_id;
#Next, we wait for it to complete
$result->wait;
#Now we should have the result but we make sure that it didn't fail and exit if it did fail.
if ($result->iserror) {
print "playback current id returned error, ", $result->get_error, "\n";
exit 2;
}
#So now we have a playback ID, let's print it out and have a look!
print "$id\n";
Not terribly exciting, let's use it to get some more info on the current track
if ($id == 0) {
print "Nothing is playing";
} else {
#Let's get the playback info for this ID
$result = $xmms->medialib_get_info($id);
#Wait for it to complete
$result->wait;
#And make sure that it didn't fail
if ($result->iserror) {
print "medialib get info returns error, ", $result->get_error, "\n";
exit 3;
}
#Let's get the info that was returned
my $minfo = $result->value;
my $artist = $minfo->{artist} || 'Unknown Artist';
my $title = $minfo->{title} || 'Unknown Title';
#And print it out
print "Currently play: $title by $artist";
}

