Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Clementine instance via D-Bus MPRIS in Java

Tags:

java

dbus

I'm using Clementine as a music player.

It can be controlled with D-Bus-commands. On the command-line, using qdbus, I can:

  • Start
  • Stop
  • Pause the player
  • Force it to skip a song in the playlist
  • Check the length of the playlist
  • Check the currently playing track in the playlist and its metadata.

I want to do this in a Java program. I tried to get things working, but somehow I do not understand it and I cannot find a piece of example code that I can use for my program.

Here's a sample session using qdbus, to give you an idea of service names et cetera:

$ qdbus org.mpris.clementine /TrackList
method int org.freedesktop.MediaPlayer.AddTrack(QString, bool)
method void org.freedesktop.MediaPlayer.DelTrack(int)
method int org.freedesktop.MediaPlayer.GetCurrentTrack()
method int org.freedesktop.MediaPlayer.GetLength()
method QVariantMap org.freedesktop.MediaPlayer.GetMetadata(int)
method void org.freedesktop.MediaPlayer.PlayTrack(int)
method void org.freedesktop.MediaPlayer.SetLoop(bool)
method void org.freedesktop.MediaPlayer.SetRandom(bool)
signal void org.freedesktop.MediaPlayer.TrackListChange(int)
method QDBusVariant org.freedesktop.DBus.Properties.Get(QString interface_name, QString property_name)
method QVariantMap org.freedesktop.DBus.Properties.GetAll(QString interface_name)
method void org.freedesktop.DBus.Properties.Set(QString interface_name, QString property_name, QDBusVariant value)
method QString org.freedesktop.DBus.Introspectable.Introspect()

$ qdbus org.mpris.clementine /TrackList GetLength
13

$ qdbus org.mpris.clementine /TrackList GetCurrentTrack
7

$ qdbus org.mpris.clementine /TrackList GetMetadata 7
album: On The Wires Of Our Nerves
artist: Add N To X
audio-bitrate: 224
audio-samplerate: 44100
genre: Other
location: /media/nas-media/Music/Add_N_to_X/On_The_Wires_Of_Our_Nerves/08-King_Wasp.ogg
mtime: 215000
time: 215
title: King Wasp
tracknumber: 8
year: 1998

I'm trying to make a program that only prints the number of the currently playing track in the playlist. I thought I needed to create an interface first, so I created something like:

package my.package;

import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusInterfaceName;

@DBusInterfaceName("org.freedesktop.MediaPlayer")
public interface TrackList extends DBusInterface {
   int GetCurrentTrack();
}

Then I used this in a test like this:

DBusConnection dc  = DBusConnection.getConnection(DBusConnection.SESSION);
TrackList trackList = (TrackList) dc.getRemoteObject("org.mpris.clementine", "/TrackList");
int currentTrack = trackList.GetCurrentTrack();
System.out.println(currentTrack);

But this produces an error for the second line: java.lang.ClassCastException: $Proxy6 cannot be cast to my.package.TrackList.

I'm sure I'm making more than one mistake. Can someone please provide input to my approach?

like image 934
fariid Avatar asked Aug 20 '26 04:08

fariid


1 Answers

Can't test it with Clementine right now, but the following approach with explicit type specified in the getRemoteObject call works for QuodLibet:

package my.sample;

import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusInterfaceName;

@DBusInterfaceName("net.sacredchao.QuodLibet")
public interface Quodlibet extends DBusInterface {
    void Play();
    void Pause();
}

Invocation:

DBusConnection dc = DBusConnection.getConnection(DBusConnection.SESSION);
Quodlibet player = dc.getRemoteObject("net.sacredchao.QuodLibet",
    "/net/sacredchao/QuodLibet", Quodlibet.class);
player.Play();
Thread.sleep(3000, 0);
player.Pause();
dc.disconnect();
like image 85
rkhayrov Avatar answered Aug 21 '26 18:08

rkhayrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!