【发布时间】:2011-07-17 16:39:57
【问题描述】:
我想做与here 中描述的相同的事情,但使用 shell 脚本(最好在 bash 中)而不是 python。使用dbus-monitor似乎应该可以实现这样的事情,但我对dbus不是很熟悉,我不清楚如何将解决方案中描述的概念应用于python问题并将它们应用于dbus-监控工具。
【问题讨论】:
我想做与here 中描述的相同的事情,但使用 shell 脚本(最好在 bash 中)而不是 python。使用dbus-monitor似乎应该可以实现这样的事情,但我对dbus不是很熟悉,我不清楚如何将解决方案中描述的概念应用于python问题并将它们应用于dbus-监控工具。
【问题讨论】:
这是我能找到的最简单的方法:
#!/bin/bash
interface=org.gnome.Rhythmbox.Player
member=playingUriChanged
# listen for playingUriChanged DBus events,
# each time we enter the loop, we just got an event
# so handle the event, e.g. by printing the artist and title
# see rhythmbox-client --print-playing-format for more output options
dbus-monitor --profile "interface='$interface',member='$member'" |
while read -r line; do
printf "Now playing: "
rhythmbox-client --print-playing
done
它产生这样的输出:
Now playing: Daft Punk - Overture
Now playing: Daft Punk - The Grid
它还会在启动时打印当前播放的歌曲。如果这不是您想要的,请查看$line 的内容,看看它是否包含NameAcquired 或playingUriChanged。如果它包含NameAcquired,请跳过它。
Python版本和这个bash版本的主要区别在于Python版本使用DBus来获取播放歌曲信息。我找不到使用 bash 的好方法,但 rhythmbox-client --print-playing 似乎效果很好。
【讨论】:
#!/bin/bash放在第一行了吗?
rhythmbox-client 在较新的系统上不存在。