答案1
该功能通过MPRIS
(媒体播放器远程接口规范),一项标准D-Bus 接口。
D-Bus 具有dbus-send
您可以手动使用 DBUS 命令来控制它,但我发现它对于日常使用来说“有点”复杂:
# 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'
(Spotify 是播放器,请进行相应更改)
playerctl
或者直接使用playerctl
:
playerctl status
playerctl metadata
安装方式apt
:
sudo apt install playerctl
Python 的 D-Bus 模块
您还可以使用python
的dbus
模块控制播放器:
#!/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)