通过Java中的D-Bus MPRIS访问Clementine实例

时间:2011-12-28 13:05:50

标签: java dbus

我正在使用Clementine作为音乐播放器。

可以使用D-Bus命令控制它。在命令行中,使用qdbus,我可以:

  • 开始
  • 停止
  • 暂停播放器
  • 强制它跳过播放列表中的歌曲
  • 检查播放列表的长度
  • 检查播放列表中当前播放的曲目及其元数据。

我想在Java程序中执行此操作。我试图让事情有效,但不知怎的,我不理解它,我找不到一段可用于我的程序的示例代码。

这是一个使用qdbus的示例会话,让您了解服务名称等等:

$ 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

我正在尝试制作一个只打印播放列表中当前播放曲目编号的程序。我以为我需要先创建一个接口,所以我创建了类似的东西:

package my.package;

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

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

然后我在这样的测试中使用了它:

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

但这会在第二行产生错误:java.lang.ClassCastException: $Proxy6 cannot be cast to my.package.TrackList

我确定我犯了不止一个错误。有人可以为我的方法提供意见吗?

1 个答案:

答案 0 :(得分:2)

目前无法使用Clementine进行测试,但getRemoteObject调用中指定的显式类型的以下方法适用于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();
}

调用:

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();