# Get current Status
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify \
/org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get \
string:'org.mpris.MediaPlayer2.Player' \
string:'PlaybackStatus'
# Get Metadata of currently playing song (if Playing)
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify \
/org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get \
string:'org.mpris.MediaPlayer2.Player' \
string:'Metadata'
#!/usr/bin/env python3
import dbus
bus = dbus.SessionBus()
for service in bus.list_names():
if service.startswith('org.mpris.MediaPlayer2.'):
player = dbus.SessionBus().get_object(service, '/org/mpris/MediaPlayer2')
status=player.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus', dbus_interface='org.freedesktop.DBus.Properties')
print(status)
metadata = player.Get('org.mpris.MediaPlayer2.Player', 'Metadata', dbus_interface='org.freedesktop.DBus.Properties')
print(metadata)
该功能通过标准D-Bus 接口
MPRIS
(媒体播放器远程接口规范)实现。D-总线与
dbus-send
您可以使用 DBUS 命令手动控制它,但我发现它对于日常使用“有点”复杂:
(Spotify 是播放器,请相应更改)
playerctl
或者干脆使用
playerctl
:安装
apt
:Python 的 D-Bus 模块
您还可以使用
python
'sdbus
模块控制播放器: