在Python中使用org.mpris.mediaplayer2.player的PlaybackStatus属性

3 投票
1 回答
2174 浏览
提问于 2025-04-18 04:28

这个规范页面讲的是这个特定接口的内容:

PlaybackStatus — s (Playback_Status)
.
.
.
May be "Playing", "Paused" or "Stopped".

但是当我这样阅读的时候:

print "Song %s" % iPlayer.PlaybackStatus

或者

if iPlayer.PlaybackStatus == "Playing":
    print "Song playing"

它显示了一个很奇怪的输出,比如 <dbus.proxies._ProxyMethod instance at 0x255f248>

我该如何获取这个变量的字符串值呢?

1 个回答

7

你需要调用 Get 方法来获取这个属性。这个方法会返回一个字符串。我是这样做的,以获取 VLC 播放器的播放状态:

import dbus

bus = dbus.SessionBus()
vlc_media_player_obj = bus.get_object("org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2")
props_iface = dbus.Interface(vlc_media_player_obj, 'org.freedesktop.DBus.Properties')
pb_stat = props_iface.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus')

在我的情况下(希望在你的情况下也是这样),这个对象还拥有一个 org.freedesktop.DBus.Properties 接口,这个接口里有 Get 方法,你可以像上面那样调用它。

撰写回答