通过Java中的D-Bus MPRIS访问Clementine实例
我正在使用Clementine作为音乐播放器。
I'm using Clementine as a music player.
可以用D-Bus命令控制。在命令行上,使用qdbus,我可以:
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.
我想在Java程序中执行此操作。我试图使事情正常运行,但是由于某种原因我不理解它,因此找不到适合我的程序的示例代码。
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.
这里是一个示例会话使用qdbus,可以使您了解服务名称等:
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);
但这会在第二行产生错误: java.lang.ClassCastException :$ Proxy6无法转换为my.package.TrackList
。
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?
目前无法使用Clementine对其进行测试,但是以下方法具有明确的含义在 getRemoteObject
调用中指定的类型适用于QuodLibet:
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();
}
调用:
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();